13 December 2005

Merge arrays in php

The short answer is function array_merge from php manual.

Let say I have 2 arrays to merge:

$a1 = array('key1'=>'val1', '33'=>'dummy_value');
$a2 = array('choose_one'=>'');
$result = array_merge($a1, $a2);

What my expected result is:

array('key1'=>'val1', '33'=>'dummy_value', 'choose_one'=>'');

Right? Emm... my expectation is wrong, the actual result is, when print_r($result):

Array
(
[key1] => val1
[0] => dummy_value
[choose_one] =>
)

What's wrong? After re-read the fine manual, the following line interest me:

Don't forget that numeric keys will be renumbered!

What? I put the key as '33' (with quotes), that should be string. Emmmm... no idea. I am suck or php suck?

2 comments:

Anonymous said...

check out PHP Array

to quote from the php manual
"If a key is the standard representation of an integer, it will be interpreted as such (i.e. "8" will be interpreted as 8, while "08" will be interpreted as "08")."

Anonymous said...

try to check for a function named fuzzy_fieldname (what a name ..) in ******. that was one of my fun moments with array in php :)