Welcome Guest, Not a member yet? Register   Sign In
array_merge() is messing with my array keys.

Hi everyone. I'm having trouble keeping the keys in the right places in an array of company names. I am sure I am falling victim to my own stupidity, so please forgive me if I am missing something obvious.

I've put it into a controller to hopefully make it easier for anyone to test.


class Test extends Controller {

    function __construct()

    function index($params="") {

         * Let's imagine that this is an array of companies passed back
         * from my model.
         * This is going to be used in a dropdown box, and the ID is
         * used as the value.
        $arr = array(
            '3' => 'a company',
            '5' => 'b company',
            '4' => 'c company',
            '2' => 'd company',
            '1' => 'e company'

        echo 'Here we can see that the array keeps the order specified above.<br />';
        echo '<pre>',print_r($arr),'</pre>';

        echo 'Now I merge this array with another array because I need an "Other" option<br />';
        $arr = array_merge($arr, array('other' => 'Other'));

        echo 'Here\'s the new array:<br />';
        echo '<pre>',print_r($arr),'</pre>';

        echo 'Hmmmm... What\'s happened to my array? The keys have been changed!';

For those who are happy to take my word for it, the first print_r($arr) displays:

    [3] => a company
    [5] => b company
    [4] => c company
    [2] => d company
    [1] => e company

After the array is merged with this one:

array('other' => 'Other');

...this is the result of the second print_r($arr):

    [0] => a company
    [1] => b company
    [2] => c company
    [3] => d company
    [4] => e company
    [other] => Other

Is this PHP being strange, or is it just me being strange? Can anyone else confirm this? I am running "PHP 5.2.4-2ubuntu5.3 with Suhosin-Patch", straight from the Ubuntu repositories.

I do have a workaround, and that is to simply append some text to the beginning of the option values, but then I will have to strip it on the server. It's easily done, but is it really necessary?

From php.net :
Quote:If only one array is given and the array is numerically indexed, the keys get reindexed in a continuous way.
instead of using array_merge you could use array_push.

That's why I am confused, there are "two" arrays, not just one.

I'm not entirely sure how I can use array_pop() to merge to add an extra option to the array. Did you mean array_splice()? If not, please could you give an example?

Fuzzy sight too Wink i wrote array_push : Push one or more elements onto the end of array.

But with array_merge the reindexing of the keys happens too with multiple arrays if you check out the example on the page
$array1 = array("color" => "red", 2, 4);
$array2 = array("a", "b", "color" => "green", "shape" => "trapezoid", 4);
$result = array_merge($array1, $array2);

/*The above example will output:

    [color] => green
    [0] => 2
    [1] => 4
    [2] => a
    [3] => b
    [shape] => trapezoid
    [4] => 4

OK, problem solved!

array_merge() was overkill. I just needed to add the entry to the array in the normal fashion:

$arr['other'] = 'Other';

Why it never occurred to me, I have no idea, but if I had two large arrays to merge, the problem would still be there, as array_splice() seems to suffer from the same problem.

Haha! Whoops. Thanks for pointing that out, you are correct of course. Everything is working as I would expect it to now. Thanks for your help. You have found my only weakness - stupidity...

To quote Vinnie Jones from the movie "Snatch": Never underestimate the predictability of stupidity...

Theme © iAndrew 2016 - Forum software by © MyBB