Home > Archive > PHP Language > August 2007 > array_merge bug ?
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
| cornelius 2007-08-07, 6:59 pm |
| Hi,
This is my code :
########
$Table1 = array('test' => 1, '432' => 2) ;
$Table2 = array('test2' => 1) ;
$Result = array_merge($Table1, $Table2) ;
echo "<pre>Table1=" . print_r($Table1, 1) . "</pre>";
echo "<pre>Table2=" . print_r($Table2, 1) . "</pre>";
echo "<pre>Result=" . print_r($Result, 1) . "</pre>";
########
And the result is :
########
Table1=Array
(
[test] => 1
[432] => 2
)
Table2=Array
(
[test2] => 1
)
Result=Array
(
[test] => 1
[0] => 2
[test2] => 1
)
########
The '432' key has become 0 !
Is it a bug (I couldn't find it over google) ?
How to keep the '432' key in the result ?
Thanks in advance,
// Cornelius
| |
| cornelius 2007-08-07, 6:59 pm |
| I've just found the solution :
$Result = $Table1 + $Table2 ;
is better than :
$Result = array_merge($Table1, $Table2) ;
// Cornelius
| |
|
| On Tue, 07 Aug 2007 19:12:15 +0200, cornelius <cornelius@nospam.org> wro=
te:
> I've just found the solution :
>
> $Result =3D $Table1 + $Table2 ;
>
> is better than :
>
> $Result =3D array_merge($Table1, $Table2) ;
Depends on wether you want to preserve the key :P
For future reference/people finding this thread: array_merge reindexes =
numerical array's, and on a merge will _overwrite_ string keys but =
_append_ numerical keys.
-- =
Rik Wasmus
|
|
|
|
|