2

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 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

I just noticed this and fixed it. Thank You, but the question remains.
0

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

3 Comments

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???
Or even build an entirely different array and than set $array to that other built array??
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...

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.