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

[eluser]TheFuzzy0ne[/eluser]
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.

Code:
<?php

class Test extends Controller {

    function __construct()
    {
        parent::Controller();
    }

    function index($params="") {
        $this->load->helper('form');

        /**
         * 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:

Code:
Array
(
    [3] => a company
    [5] => b company
    [4] => c company
    [2] => d company
    [1] => e company
)

After the array is merged with this one:

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

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

Code:
Array
(
    [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 0.9.6.2", 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?
#2

[eluser]xwero[/eluser]
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.
#3

[eluser]TheFuzzy0ne[/eluser]
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?
#4

[eluser]xwero[/eluser]
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
Code:
&lt;?php
$array1 = array("color" => "red", 2, 4);
$array2 = array("a", "b", "color" => "green", "shape" => "trapezoid", 4);
$result = array_merge($array1, $array2);
print_r($result);
?&gt;

/*The above example will output:

Array
(
    [color] => green
    [0] => 2
    [1] => 4
    [2] => a
    [3] => b
    [shape] => trapezoid
    [4] => 4
)*/
#5

[eluser]TheFuzzy0ne[/eluser]
OK, problem solved!

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

Code:
$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.
#6

[eluser]TheFuzzy0ne[/eluser]
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