将多维数组添加到其他多维数组的子数组中

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了将多维数组添加到其他多维数组的子数组中相关的知识,希望对你有一定的参考价值。

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

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

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

答案

您可以使用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));

以上是关于将多维数组添加到其他多维数组的子数组中的主要内容,如果未能解决你的问题,请参考以下文章

在循环内将数组添加到多维数组

如何将多维数组添加到现有的 Spark DataFrame

如何将哈希表添加到多维数组?无法通过成员枚举赋值

将项目添加到 Mongoose 中的多维对象数组

将项目添加到 Mongoose 中的多维对象数组

PHP使用array_push将元素添加到多维数组