PHP array_merge() συνάρτηση

Παράδειγμα

Συγχώνευση δύο πινάκων σε έναν πίνακα:

<?php
$a1=array("red","green");
$a2=array("blue","yellow");
print_r(array_merge($a1,$a2));
?>

Run Instances

Ορισμός και χρήση

Η συνάρτηση array_merge() συνδυάζει έναν ή περισσότερους πίνακες σε έναν πίνακα.

Tip:Μπορείτε να εισάγετε μια ή περισσότερες πίνακες στη συνάρτηση.

Note:If two or more array elements have the same key name, the last element will overwrite the other elements.

Note:If you input only one array to the array_merge() function and the key name is an integer, the function will return a new array with integer keys starting from 0 (see the example below 1).

Tip:This function is similar to array_merge_recursive() The difference between functions is in handling the case where two or more array elements have the same key name. array_merge_recursive() does not overwrite key names but recursively forms an array of values with the same key names.

Syntax

array_merge(array1,array2,array3...)
Parameters Description
array1 Required. Specify an array.
array2 Optional. Specify an array.
array3 Optional. Specify an array.

Technical Details

Return Value: Returns the merged array.
PHP Version: 4+
Update Log: Since PHP 5.0, this function only accepts array type parameters.

More Examples

Example 1

Merge two associative arrays into one array:

<?php
$a1=array("a"=>"red","b"=>"green");
$a2=array("c"=>"blue","b"=>"yellow");
print_r(array_merge($a1,$a2));
?>

Run Instances

Example 2

Use only one array parameter with integer keys:

<?php
$a=array(3=>"red",4=>"green");
print_r(array_merge($a));
?>

Run Instances