views:

92

answers:

3

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 :)

+2  A: 

You are trying to have an array with two identical keys 'love'. This is not possible.

EDIT:

You can do:

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

working example

codaddict
I just noticed this and fixed it. Thank You, but the question remains.
SoLoGHoST
Thank You very Much, and thanks for the excellent example :)
SoLoGHoST
A: 

It seemed to me that you didn't understand that in PHP all arrays are hashes (associative arrays). So the order can't be influenced. If you need to have a particular order you need to use sort etc. to define a particular order or use a simple array

$order = array ('love', 'happiness', 'pease');

Use the $order array to access $array. In the $order array the keys are 1, 2, 3...

khmarbaise
So wait, you are saying that you can't use a foreach to append the $another_array into the $array? I mean, couldn't you use the key() function somehow and current() and perhaps next() php functions???
SoLoGHoST
Or even build an entirely different array and than set $array to that other built array??
SoLoGHoST
So none of this is possible?
SoLoGHoST
You can use foreach etc. but you can't define the order of the entries within the array...the only possible solution is to create a separate array (like i mentioned) to define the order and using the entries in the order array to access the $array...
khmarbaise
BTW: Missed the doc link http://de3.php.net/manual/en/language.types.array.php
khmarbaise
A: 

Maybe something like ?

$array; // data from question
$another_array; // data from question

$array = pushElement($array, $another_array, 'happiness', true);


function pushElement($array, $another_array, $key, $after = true)
{
  $result = array();

  foreach ( $array as $k => $v ) {

    if ( $after === true ) {
      $result[$k] = $v;
    }

    if ( $k == $key ) {
      foreach ( $another_array as $kk => $vv ) {
        $result[$kk] = $vv;
      }
    }

    if ( $after === false ) {
      $result[$k] = $v;
    }

  }

  return $result;
};
hsz