合并两个多维数组 [英] Merge two multi-dimensional arrays

查看:104
本文介绍了合并两个多维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个MySQL表 types 存储产品类型。我将它们全部获取并得到以下数组:

I have a MySQL table typeswhere I store product types. I fetch them all and get this array:

 [0]=>
    ['unique_codename']=>'cars'
    ['category']=>'vehicle'
    …some other stuf…
 [1]=>
    ['unique_codename']=>'fruit'
    ['category']=>'food'
    …some other stuf…
 [2]=>
    ['unique_codename']=>'vegetables'
    ['category']=>'food'
    …some other stuf…
…

接下来,我有一个表,其中包含特定的产品。我可以全部获取并获取:

Next, I have a table which contains specific products. I could fetch them all and get:

 [0]=>
    ['codename']=>'fruit'
    ['name']=>'banana'
    …some other stuf…
 [1]=>
    ['codename']=>'fruit'
    ['name']=>'apple'
    …some other stuf…
 [2]=>
    ['codename']=>'vegetables'
    ['name']=>'cauliflower'
    …some other stuf…
 [3]=>
    ['codename']=>'cars'
    ['name']=>'audi'
    …some other stuf…
 [4]=>
    ['codename']=>'cars'
    ['name']=>'volvo'
    …some other stuf…
…

我想构造一个包含所有信息的单个数组,如下所示:

I want to construct one single array that contains all the info, like so:

 [0]=>
    ['unique_codename']=>'cars'
    ['sorts']=>
    [0]=>
        ['name'] = 'audi'
        …
    [1]=>
        ['name'] = 'volvo'
        …
    ['category']=>'vehicle'
    …

 [1]=>
    ['unique_codename']=>'fruit'
    ['sorts']=>
    [0]=>
        ['name'] = 'banana'
        …
    [1]=>
        ['name'] = 'apple'
        …
    ['category']=>'food'
    …

 [2]=>
    ['unique_codename']=>'vegetables'
    ['sorts']=>
    [0]=>
        ['name'] = 'cauliflower'
        …
    ['category']=>'food'
    …
…

我在考虑先获取两个数组。接下来,我可以在适当的位置将第二个数组推入第一个数组,但是我无法弄清楚在使用 array_push 时如何工作。谁能帮助我?还是有一个更优雅的解决方案来实现此目的?

I was thinking about first fetching both arrays. Next, I could push the second array in the first in the appropriate place, but I can't figure out how that would work when using array_push. Anyone who can help me? Or is there a more elegant solution to accomplish this?

推荐答案

您要执行的操作是将多维数组按特定对象分组列,然后将其合并。

What you want to do is grouping an multi dimensional array by a specific column and then merge it.

可以使用 array_reduce 进行分组,方法是创建一个新的关联数组,

The grouping can be done with array_reduce, by creating a new associative array, that uses the categories as keys.

$groupedProducts = array_reduce($products, function($carry, $product){
    if (!array_key_exists($product['codename'], $carry)) {
        $carry[$product['codename']] = [];
    }
    $carry[$product['codename']][] = $product;
    return $carry;
}, []);

这将创建以下数据结构:

This will create the following data structure:

$groupedProducts = [
    'fruit' => [
        [
            'codename' => 'fruit',
            'name' => 'banana',
            ... some other stuff ...
        ],
        [
            'codename' => 'fruit',
            'name' => 'apple',
            ... some other stuff ...
        ]
    ],
    'vegetables' => [
        [
            'codename' => 'vegetables',
            'name' => 'cauliflower',
            ... some other stuff ...
        ]
    ],
    'cars' => [
        [
            'codename' => 'cars',
            'name' => 'audi',
            ... some other stuff ...
        ],
        [
            'codename' => 'cars',
            'name' => 'volvo',
            ... some other stuff ...
        ]
    ],
]

如您所见,所有产品均按代号分组。如果您不希望内部数组中的代号键,则可以在 array_reduce 匿名函数中取消设置或使用 array_intersect_key 在其中选择要保留的特定密钥。

As you can see all products are grouped by the codename. If you do not want the codename key in the inner arrays you can unset it in the array_reduce anonymous function, or use array_intersect_key in there to select specific keys you want to keep.

如果您不做任何调整,就可以重复使用代码。还要按类别对最终数组进行分组。

You can reuse the code with little adjustments if you want to group the final array by categories as well.

接下来是合并。在这里,您可以在初始数组上使用 array_map 向其中添加排序:

Next comes the merge. Here you can use array_map on the initial array to add the sorts to it:

$finalArray = array_map(function($type) use ($groupedProducts) {
    if (array_key_exists($type['unique_codename'], $groupedProducts)) {
       $type['sorts'] = $groupedProducts[$type['unique_codename']];
    }
    return $type;
}, $types);

此代码将创建您要构建的最终数组。

This code will create the final array you wanted to build.

这篇关于合并两个多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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