PHP将多维数组展平,同时保持索引与相同的键相关

PHP将多维数组展平,同时保持索引与相同的键相关,php,arrays,merge,Php,Arrays,Merge,我有以下数组: ( [0] => Array ( [items] => Array ( [0] => Array ( [snippet] => text1 ) [1] => Array ( [snippet] => text2 ) [2] => Array ( [snippet] => text3 )

我有以下数组:

(
[0] => Array
    (
        [items] => Array
            (
                [0] => Array ( [snippet] => text1 )
                [1] => Array ( [snippet] => text2 )
                [2] => Array ( [snippet] => text3 )
            )
    )

[1] => Array
    (
        [items] => Array
            (
                [0] => Array ( [snippet] => text4 )
                [1] => Array ( [snippet] => text5 )
                [2] => Array ( [snippet] => text6 )
            )

    )  
 )
我想把它变成这样:

Array
(
[items] => Array
    (
        [0] => Array ( [snippet] => text )
        [1] => Array ( [snippet] => text )
        [2] => Array ( [snippet] => text )
        [3] => Array ( [snippet] => text )
        [4] => Array ( [snippet] => text )
        [6] => Array ( [snippet] => text )
    )
)
数组中可以有更多的“项”层,“片段”层中可以有数组,我还需要保留第一个“项”数组的索引,并按照上面所示继续其余的数组,我无法理解,请提供任何帮助。

您可以使用来实现这一点:

$arr = [
    [
        'items' => [['snippet' => 'text1'],['snippet' => 'text2'],['snippet' => 'text3']]
    ],
    [
        'items' => [['snippet' => 'text4'],['snippet' => 'text5'],['snippet' => 'text6']]
    ]
];

print_r(array_merge_recursive(...$arr));
给出:

Array
(
    [items] => Array
        (
            [0] => Array
                (
                    [snippet] => text1
                )

            [1] => Array
                (
                    [snippet] => text2
                )

            [2] => Array
                (
                    [snippet] => text3
                )

            [3] => Array
                (
                    [snippet] => text4
                )

            [4] => Array
                (
                    [snippet] => text5
                )

            [5] => Array
                (
                    [snippet] => text6
                )

        )

)

还有,这是打字错误吗

  • 数组的索引为0,1,2,3,4,6->缺少5

  • text1,text2,text3。。。仅替换为
    文本

  • 您可以使用来实现这一点:

    $arr = [
        [
            'items' => [['snippet' => 'text1'],['snippet' => 'text2'],['snippet' => 'text3']]
        ],
        [
            'items' => [['snippet' => 'text4'],['snippet' => 'text5'],['snippet' => 'text6']]
        ]
    ];
    
    print_r(array_merge_recursive(...$arr));
    
    给出:

    Array
    (
        [items] => Array
            (
                [0] => Array
                    (
                        [snippet] => text1
                    )
    
                [1] => Array
                    (
                        [snippet] => text2
                    )
    
                [2] => Array
                    (
                        [snippet] => text3
                    )
    
                [3] => Array
                    (
                        [snippet] => text4
                    )
    
                [4] => Array
                    (
                        [snippet] => text5
                    )
    
                [5] => Array
                    (
                        [snippet] => text6
                    )
    
            )
    
    )
    

    还有,这是打字错误吗

  • 数组的索引为0,1,2,3,4,6->缺少5

  • text1,text2,text3。。。仅替换为
    文本


  • 这个很好用!你能解释一下这3个点在“…$arr”上的作用吗?它被称为
    splat
    操作符,用于解压参数:splat操作符是PHP5.6中发布的功能,它工作得很好!你能解释一下这3个点在“…$arr”上做了什么吗?它被称为
    splat
    操作符,用于解压参数:splat操作符是PHP5.6中发布的特性