php - Need to add an array into another array at a specified key value -


ok, have array so, it's not guaranteed laid out in order of 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, , i'd throw in array right after happiness key defined. can't use key of "peace" since must go directly after happiness, , peace might not come after happiness array changes.

so here's need add after happiness...

$another_array['love'] = array(     'info' => 'some info',     'info2' => 'more info',     'value' => 'the value of love' ); 

so final output after gets inputted directly after happiness should 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 please give me hand this. using array_shift, array_pop, or array_merge doesn't me @ all, since these go @ beginning , @ end of array. need place directly after key position within $array.

thanks :)

you trying have array 2 identical keys 'love'. not possible.

edit:

you can do:

$new_array = array(); foreach($array $k => $v) {         $new_array[$k] = $v;         if($k == 'happiness') {                 $new_array['love'] = $another_array['love'];         } } 

working example


Comments

Popular posts from this blog

javascript - Enclosure Memory Copies -

php - Replacing tags in braces, even nested tags, with regex -