Inserting a multidimensional array into another multidimensional array

余生颓废 提交于 2020-01-16 19:57:25

问题


$stack = array(
    'name'           => 'some data',
    'caption'        => 'some data',
    'published'      => 'some data',
    'updated_at'     => 'some data',
    'updated_by'     => 'some data'
);

$data = array('album_id' => 'someID');

How do i insert the data array into the stack array?

update: i tried array_unshift but it inserted the $data array in a second dimension within the multi but i want it at the same level as the others.

also, one more question

if I have another array like data and i want to insert it into the 3rd position how would i do that?


回答1:


Try

$stack = $stack + $data;

Or

$stack =array_merge($stack, $data);

If you want to add $data to the 3rd position in $stack

$chunks = array_chunk($stack, 2, true);
$stack  = array_shift($chunks);
$stack  += $data;
foreach ($chunks as $chunk) { $stack += $chunk; }


来源:https://stackoverflow.com/questions/13116562/inserting-a-multidimensional-array-into-another-multidimensional-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!