For Programmers: Free Programming Magazines  


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]

 

Author array_merge bug ?
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


Rik

2007-08-07, 6:59 pm

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
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com