array_merge() is messing with my array keys. - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: array_merge() is messing with my array keys. (/showthread.php?tid=12016) |
array_merge() is messing with my array keys. - El Forum - 10-02-2008 [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 For those who are happy to take my word for it, the first print_r($arr) displays: Code: Array After the array is merged with this one: Code: array('other' => 'Other'); ...this is the result of the second print_r($arr): Code: Array 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? array_merge() is messing with my array keys. - El Forum - 10-02-2008 [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. array_merge() is messing with my array keys. - El Forum - 10-02-2008 [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? array_merge() is messing with my array keys. - El Forum - 10-02-2008 [eluser]xwero[/eluser] Fuzzy sight too 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: <?php array_merge() is messing with my array keys. - El Forum - 10-02-2008 [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. array_merge() is messing with my array keys. - El Forum - 10-02-2008 [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... |