up vote 2 down vote favorite
1

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

link|flag

3 Answers

up vote 2 down vote accepted

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

link|flag
I just noticed this and fixed it. Thank You, but the question remains. – SoLoGHoST Apr 27 at 10:28
Thank You very Much, and thanks for the excellent example :) – SoLoGHoST Apr 27 at 10:40
up vote 0 down vote

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...

link|flag
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 Apr 27 at 10:33
Or even build an entirely different array and than set $array to that other built array?? – SoLoGHoST Apr 27 at 10:34
So none of this is possible? – SoLoGHoST Apr 27 at 10:34
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 Apr 27 at 11:34
BTW: Missed the doc link de3.php.net/manual/en/language.types.array.php – khmarbaise Apr 27 at 11:35
up vote -1 down vote

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

Your Answer

 
or
never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.