Exploit
Exploit

Reputation: 6386

Inserting a multidimensional array into another multidimensional array

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

Upvotes: 0

Views: 153

Answers (1)

air4x
air4x

Reputation: 5683

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

Upvotes: 3

Related Questions