Need to add an array into another array at a specified key value
Posted
by sologhost
on Stack Overflow
See other posts from Stack Overflow
or by sologhost
Published on 2010-04-27T10:23:07Z
Indexed on
2010/04/27
10:33 UTC
Read the original article
Hit count: 437
Ok, I have an array like so, but it's not guaranteed to be laid out in this order all of the time...
$array = array(
'sadness' => array(
'info' => 'some info',
'info2' => 'more info',
'value' => 'value',
),
'happiness' => array(
'info' => 'some info',
'info2' => 'more info',
'value' => 'the value',
),
'peace' => array(
'info' => 'some info',
'info2' => 'more info',
'value' => 'the value',
)
);
Ok, and I'd like to throw in this array right after the happiness key is defined. I can't use the key of "peace" since it must go directly after happiness, and peace might not come after happiness as this array changes.
So here's what I need to add after happiness...
$another_array['love'] = array(
'info' => 'some info',
'info2' => 'more info',
'value' => 'the value of love'
);
So the final output after it gets inputted directly after happiness should look like this:
$array = array(
'sadness' => array(
'info' => 'some info',
'info2' => 'more info',
'value' => 'value',
),
'happiness' => array(
'info' => 'some info',
'info2' => 'more info',
'value' => 'the value',
),
'love' => array(
'info' => 'some info',
'info2' => 'more info',
'value' => 'the value of love',
),
'peace' => array(
'info' => 'some info',
'info2' => 'more info',
'value' => 'the value',
)
);
Can someone please give me a hand with this. Using array_shift, array_pop, or array_merge doesn't help me at all, since these go at the beginning and at the end of the array. I need to place it directly after a KEY position within $array.
Thanks :)
© Stack Overflow or respective owner