在第二级阵列合并 [英] Array merge at second level

查看:129
本文介绍了在第二级阵列合并的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个阵列,如下图所示。我需要合并数组的内容,以便我可以得到截至去年给出的第三个数组中的结构。我已经检查array_merge但想不通这是可能的方式。任何帮助AP preciated。谢谢你。

I have two arrays as shown below. I need to merge the content of the arrays so that I can get the structure as shown in the third array at last. I have checked array_merge but can't figure out the way this is possible. Any help appreciated. Thanks.

array(
    (int) 0 => array(
        'gross_value' => '100',
        'quantity' => '1'
    ),
    (int) 1 => array(
        'gross_value' => '200',
        'quantity' => '1'
    )
)

array(
(int) 0 => array(
    'item_title_id' => '1',
    'order_id' => '4'
),
(int) 1 => array(
    'item_title_id' => '2',
    'order_id' => '4'
)
)

我应该得到一个合并的数组像这样

I should get a merged array like this

array(
(int) 0 => array(
    'gross_value' => '100',
    'quantity' => '1',
    'item_title_id' => '1',
    'order_id' => 4
),
(int) 1 => array(
    'gross_value' => '200',
    'quantity' => '1',
    'item_title_id' => '2',
    'order_id' => 4
)

推荐答案

使用的 array_merge_recursive

转换所有数字键为字符串,(做是关联数组)

Convert all numeric key to strings, (make is associative array)

$result = array_merge_recursive($ar1, $ar2);
print_r($result);

请参阅 rel=\"nofollow\">现场演示

See live demo here

这篇关于在第二级阵列合并的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆