Tuesday, January 20, 2009

How to merge two arrays together in php

When you need to join or merge two arrays together you can use the php array_merge() function to do it. This function will return the merged array.
Look at the following code and see how the index reassigned.


<?php

$first_array = array(1, 2, 3, 4, 5, 6);
$second_array = array(10, 9, 7, 3, 5);

$new_array = array_merge($first_array, $second_array);

echo '<pre>';
print_r($new_array);
echo '</pre>';

/*

OUTPUT

Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
[5] => 6
[6] => 10
[7] => 9
[8] => 7
[9] => 3
[10] => 5
)

*/

?>

No comments:

Post a Comment