将数组插入数组集合而不循环

我有两个数组..


阵列 1


$arr1 = array(

    'task' => 'delete'

)

阵列 2


$arr2 = array(

            array(

                'id'    => '1', 

                'type'  => 'post',

            ),

            array(

                'id'    => '2', 

                'type'  => 'category',

            ),

            array(

                'id'    => '1', 

                'type'  => 'tag',

            ),

        );

我如何将 Array 1 插入到所有 Array 2 集合中,结果是这样。


$arr2 = array(

            array(

                'id'    => '1',

                'task' => 'delete',

                'type'  => 'post',

            ),

            array(

                'id'    => '2', 

                'task' => 'delete',

                'type'  => 'category',

            ),

            array(

                'id'    => '1', 

                'task' => 'delete',

                'type'  => 'tag',

            ),

        );

我可以很容易地通过使用循环来获得结果,但我想不使用循环来实现它。有什么建议吗?谢谢 :)


阿晨1998
浏览 162回答 3
3回答

白衣非少年

为了将array2中的所有索引推入array_walk,您可以将array_walk与数组合并结合使用,例如,您可以查看下面的代码&nbsp; &nbsp; <?php&nbsp; &nbsp;$array1 = array(&nbsp; &nbsp; 'task' => 'delete',&nbsp; &nbsp; 'before' =>'test');&nbsp; &nbsp; $array2=array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'id'&nbsp; &nbsp; => '1',&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'type'&nbsp; => 'post',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'id'&nbsp; &nbsp; => '2',&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'type'&nbsp; => 'category',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'id'&nbsp; &nbsp; => '1',&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'type'&nbsp; => 'tag',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; array_walk($array2, function(&$newarray) use ($array1) {&nbsp; &nbsp; &nbsp; &nbsp; $newarray = array_merge($newarray, $array1);&nbsp; &nbsp; });print_r($array2);?>array_walk - 将用户提供的函数应用于数组的每个成员,而 array_merge,将两个数组合并为一个数组结果Array ([0] => Array ( [id] => 1 [type] => post [task] => delete [before] => test )&nbsp;[1] => Array ( [id] => 2 [type] => category [task] => delete [before] => test )[2] => Array ( [id] => 1 [type] => tag [task] => delete [before] => test ) )Git 中心https://github.com/fahadpatel/insert-array-into-array-without-loop

皈依舞

您可以使用array_walk跳过编写循环。$arr1 = array(&nbsp; &nbsp; 'task' => 'delete');$arr2 = array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'id'&nbsp; &nbsp; => '1',&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'type'&nbsp; => 'post',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'id'&nbsp; &nbsp; => '2',&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'type'&nbsp; => 'category',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'id'&nbsp; &nbsp; => '1',&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'type'&nbsp; => 'tag',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ),&nbsp; &nbsp; &nbsp; &nbsp; );array_walk($arr2, function(&$item) use ($arr1) {&nbsp; &nbsp; $item = array_merge($item, $arr1);});array_walk 您可以在此处查看文档。

开满天机

您可以使用array_map. 虽然它在幕后循环(没有真正的解决方法),但至少你不必自己做:$arr3 = array_map(static function ($entry) use ($arr1) {&nbsp; return $entry + $arr1;}, $arr2);PHP 7.4 版本:$arr3 = array_map(static fn($entry) => $entry + $arr1, $arr2);演示:https ://3v4l.org/2u2SR
打开App,查看更多内容
随时随地看视频慕课网APP