Wednesday 28 January 2009
Facebook StumbleUpon Twitter Google+ Pin It

Merging Arrays Preserving the Numeric Keys

Merging arrays like this:
<?php
    $array1 = array();
    $array2 = array(1 => "data");
    $result = array_merge($array1, $array2);
?>
will result to:
Array
(
    [0] => data
)
Don't forget that numeric keys will be renumbered!

If you want to completely preserve the arrays and just want to append them to each other (not overwriting the previous keys), use the + operator:

<?php
    $array1 = array();
    $array2 = array(1 => "data");
    $result = $array1 + $array2;
?>
The numeric key will be preserved and thus the association remains.

Array
(
   [1] => data
)
Parthiv Patel
Bhaishri Info Solution
Sr. PHP Developer
Limdi Chowk, AT PO. Nar, Di. Anand
Nar, Gujarat
388150
India
pparthiv2412@gmail.com
7383343029
DOB: 12/24/1986

No comments: