dqy13020940 2017-12-22 22:24
浏览 54
已采纳

合并两个多维数组

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'
    …
…

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?

  • 写回答

2条回答 默认 最新

  • dongshipang8094 2017-12-22 23:01
    关注

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

    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 ...
            ]
        ],
    ]
    

    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.

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?