Jesus · Bible · HTML · CSS · JS · PHP · SVG · Applications

array_merge

Description

Merge one or more arrays

If the input arrays have the same string keys, then the later value for that key will overwrite the previous one. If, however, the arrays contain numeric keys, the later value will not overwrite the original value, but will be appended.

Syntax

array_merge(
    array ...$arrays
): array

Parameters

arrays

Variable list of arrays to merge.

Return

Returns the resulting array. If called without any arguments, returns an empty array.

Examples

1 · void

<?

$return = array_merge();

print_r($return);

?>
Array
(
)

2 · arrays · one · indexed

<?

$arrays = array(0, 1);

$return = array_merge($arrays);

print_r($return);

?>
Array
(
    [0] => 0
    [1] => 1
)

3 · arrays · one · associative

<?

$arrays = array("a" => 0, "b" => 1);

$return = array_merge($arrays);

print_r($return);

?>
Array
(
    [a] => 0
    [b] => 1
)

4 · arrays · multiple · indexed

<?

$arrays1 = array(0, 1);
$arrays2 = array(2, 3);

$return = array_merge($arrays1, $arrays2);

print_r($return);

?>
Array
(
    [0] => 0
    [1] => 1
    [2] => 2
    [3] => 3
)

5 · arrays · multiple · associative

<?

$arrays1 = array("a" => 0, "b" => 1);
$arrays2 = array("c" => 2, "d" => 3);

$return = array_merge($arrays1, $arrays2);

print_r($return);

?>
Array
(
    [a] => 0
    [b] => 1
    [c] => 2
    [d] => 3
)

6 · same key · indexed

<?

$arrays1 = array(0, 1);
$arrays2 = array(0 => 2, 1 => 3);

$return = array_merge($arrays1, $arrays2);

print_r($return);

?>
Array
(
    [0] => 0
    [1] => 1
    [2] => 2
    [3] => 3
)

7 · same key · associative

<?

$arrays1 = array("a" => 0, "b" => 1);
$arrays2 = array("a" => 2, "b" => 3);

$return = array_merge($arrays1, $arrays2);

print_r($return);

?>
Array
(
    [a] => 2
    [b] => 3
)
HomeMenu