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

合并两个多维数组

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条)

相关推荐 更多相似问题