将一个多维数组插入另一个多维数组 [英] Inserting a multidimensional array into another multidimensional array

查看:58
本文介绍了将一个多维数组插入另一个多维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

$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?

更新:我尝试了 array_unshift 但它在 multi 的第二维中插入了 $data 数组,但我希望它与其他数组处于同一级别.

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.

还有一个问题

如果我有另一个类似数据的数组,我想将它插入到第三个位置,我该怎么做?

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

推荐答案

尝试

$stack = $stack + $data;

或者

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

如果你想把$data添加到$stack的第三个位置

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; }

这篇关于将一个多维数组插入另一个多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆