将多维数组添加到其他多维数组

时间:2018-05-10 08:53:35

标签: php multidimensional-array

感谢Philipp Maurer,我能够合并两个多维数组,给我这个最终数组:

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

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

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

现在,我想将这个数组合并到它:

[0]=>
    ['codename']=>'fruit'
    ['name']=>'banana'
    [‘type’]=>’yellowbanana’
    …some other stuf…
 [1]=>
    ['codename']=>'fruit'
    ['name']=>'banana'
    [‘type’]=>’brownbanana’
    …some other stuf…
 [2]=>
    ['codename']=>’cars’
    ['name']=>’audi’
    [‘type’]=>’a2’
    …some other stuf…
 [3]=>
    ['codename']=>'fruit'
    ['name']=>'banana'
    [‘type’]=>’greenbanana’
    …some other stuf…
 [4]=>
    ['codename']=>’cars’
    ['name']=>’audi’
    [‘type’]=>’a6’
    …some other stuf…
 [5]=>
    ['codename']=>’cars’
    ['name']=>’other’
    [‘type’]=>’dummytext1’
    …some other stuf…
 [6]=>
    ['codename']=>’cars’
    ['name']=>’other’
    [‘type’]=>’dummytext2’
    …some other stuf…
…

哪个应该导致最终(bis)数组:

[0]=>
    ['unique_codename']=>'cars'
    ['sorts']=>
    [0]=>
        [‘codename’] = ‘cars’
        ['name'] = 'audi'
        [‘types’]=>
        [0]=>
            [‘type’] = ‘a2’
            …
        [1]=>
            [‘type’] = ‘a6’
            …
     …
    [1]=>
        [‘codename’] = ‘cars’
        ['name'] = 'volvo'
        …
    [2]=>
        [‘codename’] = ‘cars’
        ['name'] = ‘other’
        [‘types’]=>
        [0]=>
            [‘type’] = ‘dummytext1’
            …
        [1]=>
            [‘type’] = ‘dummytext2’
            …
     …
    ['category']=>'vehicle'
    …

 [1]=>
    ['unique_codename']=>'fruit'
    ['sorts']=>
    [0]=>
        [‘codename’] = ‘fruit’
        ['name'] = 'banana'
        [‘types’]=>
        [0]=>
            [‘type’] = ‘yellowbanana’
            …
        [1]=>
            [‘type’] = ‘brownbanana’
            …
        [2]=>
            [‘type’] = ‘greenbanana’
            …
       …
    [1]=>
        [‘codename’] = ‘fruit’
        ['name'] = 'apple'
        …
    ['category']=>'food'
    …

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

我尝试过与第一次合并相同的方法,但我似乎无法做到正确。由于这可能需要一种不同的方法,我已经为它开了一个新问题。请注意,有时,如果初始子阵列尚未存在,则应将新的“名称”添加到初始子阵列中(在我的示例中为“其他”)。

任何可以帮助我的人?我自己不是阵列向导,显然......非常感谢任何帮助!

1 个答案:

答案 0 :(得分:1)

您可以使用array_combine()开始使用unique_codename作为初始数组中的键,以便在其上进行搜索。然后,您可以在新阵列上使用循环,并检查“主阵列”中是否存在codename。如果您可以在主阵列的type内添加新的types

$init = array(); // your initial array
$array = array(); // your new array

$init = array_combine(array_column($init, 'unique_codename'), $init);
foreach ($array as $k => $item) {
    if (isset($init[$item['codename']])) {
        foreach ($init[$item['codename']]['sorts'] as &$sort) { // $sort is used as reference
            if ($sort['name'] == $item['name']) {
                $sort['types'][] = $item['type'];
            }
        }
    }
}
print_r(array_values($init));

输出(见online demo):

Array
(
    [0] => Array
        (
            [unique_codename] => cars
            [sorts] => Array
                (
                    [0] => Array
                        (
                            [codename] => cars
                            [name] => audi
                            [types] => Array
                                (
                                    [0] => a2
                                    [1] => a6
                                )

                        )

                    [1] => Array
                        (
                            [codename] => cars
                            [name] => volvo
                        )

                )

            [category] => vehicle
        )

    [1] => Array
        (
            [unique_codename] => fruit
            [sorts] => Array
                (
                    [0] => Array
                        (
                            [codename] => fruit
                            [name] => banana
                            [types] => Array
                                (
                                    [0] => yellowbanana
                                    [1] => brownbanana
                                    [2] => greenbanana
                                )

                        )

数据示例:

$init = [
    [
        'unique_codename'=>'cars',
        'sorts'=>[
            ['codename' => 'cars', 'name' => 'audi'],
            ['codename' => 'cars', 'name' => 'volvo']
        ],
        'category'=>'vehicle',
    ],
    [
        'unique_codename'=>'fruit',
        'sorts'=>[
            ['codename' => 'fruit', 'name' => 'banana'],
            ['codename' => 'fruit', 'name' => 'apple'],
        ],
        'category'=>'food',
    ],
    [
        'unique_codename'=>'vegetables',
        'sorts'=>[
            ['codename' => 'vegetables', 'name' => 'cauliflower'],
        ],
        'category'=>'food',
    ],
];

$array = [
    ['codename'=>'fruit', 'name'=>'banana', 'type'=>'yellowbanana'],
    ['codename'=>'fruit', 'name'=>'banana', 'type'=>'brownbanana'],
    ['codename'=>'cars', 'name'=>'audi', 'type'=>'a2'],
    ['codename'=>'fruit', 'name'=>'banana', 'type'=>'greenbanana'],
    ['codename'=>'cars', 'name'=>'audi', 'type'=>'a6'],
    ['codename'=>'cars', 'name'=>'other', 'type'=>'dummytext1'],
    ['codename'=>'cars', 'name'=>'other', 'type'=>'dummytext2'],
];

$init = array_combine(array_column($init, 'unique_codename'), $init);
foreach ($array as $k => $item) {
    if (isset($init[$item['codename']])) {
        foreach ($init[$item['codename']]['sorts'] as &$sort) {
            if ($sort['name'] == $item['name']) {
                $sort['types'][] = $item['type'];
            }
        }
    }
}
print_r(array_values($init));