0
$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
  • These are one-dimensional arrays, not multi-dimensional arrays. They are also hashes, which do not contain numerically indexed keys, but rather named keys. So, you can't insert it into a specified position because they do not really have a position. You can use array_slice for that. array_merge is how you would combine them together into a single array. The first array you specify in array_merge is the base, and all other arrays passed to array_merge after that will be added to the end. You need to assign the result of array_merge to a new variable. Look it up on php.net for more info. Commented Oct 29, 2012 at 6:12

1 Answer 1

3

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; }
Sign up to request clarification or add additional context in comments.

1 Comment

that gave me 6 for some reason. the value 6, i tried array_unshift instead and got 6. array_merge just showed the array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.