php - 将两个数组合并或组合成单个数组

标签 php arrays merge

我有一个案例,需要创建两个数组或将两个数组合并为单个数组。

第一个数组如下:

Array // First Array
(
    [0] => Array
        (
            [hotel] => cempaka
            [vendor] => mas
        )

    [1] => Array
        (
            [hotel] => permata
            [vendor] => sip
        )
)

第二个数组:

Array
(
    [0] => Array
    (
        [order_type] => 1
        [currency] => 26
        [net] => 26000
        [rate] => 26000
        [amount] => 26000
        [bank_surcharge] => 26000
        [ticket] => 26000
        [profit] => 26000
        [selling_price] => 26000
        [description] => a
    )

[1] => Array
    (
        [order_type] => 2
        [currency] => 27
        [net] => 27000
        [rate] => 27000
        [amount] => 27000
        [bank_surcharge] => 27000
        [ticket] => 27000
        [profit] => 27000
        [selling_price] => 27000
        [description] => b
    )

[2] => Array
    (
        [order_type] => 5
        [currency] => 28
        [net] => 28000
        [rate] => 28000
        [amount] => 28000
        [bank_surcharge] => 28000
        [ticket] => 28000
        [profit] => 28000
        [selling_price] => 28000
        [description] => c
    )

[3] => Array
    (
        [order_type] => 3
        [currency] => 29
        [net] => 29000
        [rate] => 29000
        [amount] => 29000
        [bank_surcharge] => 29000
        [ticket] => 29000
        [profit] => 29000
        [selling_price] => 29000
        [description] => d
    )

[4] => Array
    (
        [order_type] => 4
        [currency] => 30
        [net] => 30000
        [rate] => 30000
        [amount] => 30000
        [bank_surcharge] => 30000
        [ticket] => 30000
        [profit] => 30000
        [selling_price] => 30000
        [description] => e
    )

[5] => Array
    (
        [order_type] => 6
        [currency] => 31
        [net] => 31000
        [rate] => 31000
        [amount] => 31000
        [bank_surcharge] => 31000
        [ticket] => 31000
        [profit] => 31000
        [selling_price] => 31000
        [description] => f
    )

[6] => Array
    (
        [order_type] => 1
        [currency] => 32
        [net] => 32000
        [rate] => 32000
        [amount] => 32000
        [bank_surcharge] => 32000
        [ticket] => 32000
        [profit] => 32000
        [selling_price] => 32000
        [description] => g
    )

[7] => Array
    (
        [order_type] => 2
        [currency] => 33
        [net] => 33000
        [rate] => 33000
        [amount] => 33000
        [bank_surcharge] => 33000
        [ticket] => 33000
        [profit] => 33000
        [selling_price] => 33000
        [description] => h
    )

[8] => Array
    (
        [order_type] => 5
        [currency] => 34
        [net] => 34000
        [rate] => 34000
        [amount] => 34000
        [bank_surcharge] => 34000
        [ticket] => 34000
        [profit] => 34000
        [selling_price] => 34000
        [description] => i
    )

[9] => Array
    (
        [order_type] => 3
        [currency] => 35
        [net] => 35000
        [rate] => 35000
        [amount] => 35000
        [bank_surcharge] => 35000
        [ticket] => 35000
        [profit] => 35000
        [selling_price] => 35000
        [description] => j
    )

[10] => Array
    (
        [order_type] => 4
        [currency] => 36
        [net] => 36000
        [rate] => 36000
        [amount] => 36000
        [bank_surcharge] => 36000
        [ticket] => 36000
        [profit] => 36000
        [selling_price] => 36000
        [description] => k
    )

[11] => Array
    (
        [order_type] => 6
        [currency] => 37
        [net] => 37000
        [rate] => 37000
        [amount] => 37000
        [bank_surcharge] => 37000
        [ticket] => 37000
        [profit] => 37000
        [selling_price] => 37000
        [description] => l
    )
)

然后这是我想要得到的输出:

Array
(
[0] => Array
    (
        [hotel] => cempaka
        [vendor] => mas
        [order_type] => 1
        [currency] => 26
        [net] => 26000
        [rate] => 26000
        [amount] => 26000
        [bank_surcharge] => 26000
        [ticket] => 26000
        [profit] => 26000
        [selling_price] => 26000
        [description] => a
    )

[1] => Array
    (
        [hotel] => cempaka
        [vendor] => mas
        [order_type] => 2
        [currency] => 27
        [net] => 27000
        [rate] => 27000
        [amount] => 27000
        [bank_surcharge] => 27000
        [ticket] => 27000
        [profit] => 27000
        [selling_price] => 27000
        [description] => b
    )

[2] => Array
    (
        [hotel] => cempaka
        [vendor] => mas
        [order_type] => 5
        [currency] => 28
        [net] => 28000
        [rate] => 28000
        [amount] => 28000
        [bank_surcharge] => 28000
        [ticket] => 28000
        [profit] => 28000
        [selling_price] => 28000
        [description] => c
    )

[3] => Array
    (
        [hotel] => cempaka
        [vendor] => mas
        [order_type] => 3
        [currency] => 29
        [net] => 29000
        [rate] => 29000
        [amount] => 29000
        [bank_surcharge] => 29000
        [ticket] => 29000
        [profit] => 29000
        [selling_price] => 29000
        [description] => d
    )

[4] => Array
    (
        [hotel] => cempaka
        [vendor] => mas
        [order_type] => 4
        [currency] => 30
        [net] => 30000
        [rate] => 30000
        [amount] => 30000
        [bank_surcharge] => 30000
        [ticket] => 30000
        [profit] => 30000
        [selling_price] => 30000
        [description] => e
    )

[5] => Array
    (
        [hotel] => cempaka
        [vendor] => mas
        [order_type] => 6
        [currency] => 31
        [net] => 31000
        [rate] => 31000
        [amount] => 31000
        [bank_surcharge] => 31000
        [ticket] => 31000
        [profit] => 31000
        [selling_price] => 31000
        [description] => f
    )

[6] => Array
    (
        [hotel] => permata
        [vendor] => sip
        [order_type] => 1
        [currency] => 32
        [net] => 32000
        [rate] => 32000
        [amount] => 32000
        [bank_surcharge] => 32000
        [ticket] => 32000
        [profit] => 32000
        [selling_price] => 32000
        [description] => g
    )

[7] => Array
    (
        [hotel] => permata
        [vendor] => sip
        [order_type] => 2
        [currency] => 33
        [net] => 33000
        [rate] => 33000
        [amount] => 33000
        [bank_surcharge] => 33000
        [ticket] => 33000
        [profit] => 33000
        [selling_price] => 33000
        [description] => h
    )

[8] => Array
    (
        [hotel] => permata
        [vendor] => sip
        [order_type] => 5
        [currency] => 34
        [net] => 34000
        [rate] => 34000
        [amount] => 34000
        [bank_surcharge] => 34000
        [ticket] => 34000
        [profit] => 34000
        [selling_price] => 34000
        [description] => i
    )

[9] => Array
    (
        [hotel] => permata
        [vendor] => sip
        [order_type] => 3
        [currency] => 35
        [net] => 35000
        [rate] => 35000
        [amount] => 35000
        [bank_surcharge] => 35000
        [ticket] => 35000
        [profit] => 35000
        [selling_price] => 35000
        [description] => j
    )

[10] => Array
    (
        [hotel] => permata
        [vendor] => sip
        [order_type] => 4
        [currency] => 36
        [net] => 36000
        [rate] => 36000
        [amount] => 36000
        [bank_surcharge] => 36000
        [ticket] => 36000
        [profit] => 36000
        [selling_price] => 36000
        [description] => k
    )

[11] => Array
    (
        [hotel] => permata
        [vendor] => sip
        [order_type] => 6
        [currency] => 37
        [net] => 37000
        [rate] => 37000
        [amount] => 37000
        [bank_surcharge] => 37000
        [ticket] => 37000
        [profit] => 37000
        [selling_price] => 37000
        [description] => l
    )
)

(这两个数组是我从通过 javascript 生成的动态字段获得的) 任何解决方案将不胜感激..谢谢

最佳答案

由于您尝试根据第二个数组的键编号合并数组,因此请使用以下内容:

foreach($second_array as $key => $value){
    if($key >= 0 && $key <= 5){
        $new_array[$key] = array_merge($second_array[$key], $first_array[0]);
    }
    if($key >= 6 && $key <= 11){
        $new_array[$key] = array_merge($second_array[$key], $first_array[1]);
    }
    //etc.......
}

关于php - 将两个数组合并或组合成单个数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19698454/

相关文章:

php - 使用 and 过滤 mysql 内连接一对多结果

php - 过滤过去24小时的sql查询

java - Java中字节数组的内存使用

python - 将数据框与不可散列的列合并

用于合并图像的 C++ 库

php - laravel 5.3 不解释 index.php

php - 如何获取 woocommerce 品牌缩略图属性?

C# 如何纠正数组中的错误输入

ruby - 在Ruby中查找数组中两个数字的每个组合的总和

python - 合并(更新\插入)pandas 数据帧的更好方法