PHP 8.6.0 Released!

array_merge

(PHP 4, PHP 5, PHP 7, PHP 8)

array_merge — Fonde uno o più array

Descrizione

function array_merge(array ...$arrays): array

Fonde gli elementi di uno o più array in modo che i valori di un array vengano accodati alla fine del precedente. Restituisce l'array risultante.

Se gli array in input hanno le stesse chiavi stringa, il valore successivo per quella chiave sovrascriverà il precedente. Se invece gli array contengono chiavi numeriche, il valore successivo non sovrascriverà il valore originale, ma verrà accodato.

I valori con chiavi numeriche degli array in input verranno rinumerati con chiavi crescenti a partire da zero nell'array risultante.

Elenco dei parametri

arrays

Lista variabile di array da fondere.

Valori restituiti

Restituisce l'array risultante. Se chiamata senza argomenti, restituisce un array vuoto.

Log delle modifiche

Versione Descrizione
7.4.0 Questa funzione ora può essere chiamata senza alcun parametro. In precedenza, era richiesto almeno un parametro.

Esempi

Example #1 Esempio di array_merge()

<?php

$array1 = ["colore" => "rosso", 2, 4];
$array2 = ["a", "b", "colore" => "verde", "forma" => "trapezio", 4];

$result = array_merge($array1, $array2);

print_r($result);

Il precedente esempio visualizzerà:

Array
(
    [colore] => verde
    [0] => 2
    [1] => 4
    [2] => a
    [3] => b
    [forma] => trapezio
    [4] => 4
)

Example #2 Esempio semplice di array_merge()

<?php

$array1 = [];
$array2 = [1 => "dati"];

$result = array_merge($array1, $array2);

print_r($result);

Si ricordi che le chiavi numeriche verranno rinumerate!

Array
(
    [0] => dati
)

Per aggiungere gli elementi del secondo array al primo array senza sovrascrivere gli elementi del primo array e senza reindicizzare, usare l'operatore di unione di array +:

<?php

$array1 = [0 => 'zero_a', 2 => 'due_a', 3 => 'tre_a'];
$array2 = [1 => 'uno_b', 3 => 'tre_b', 4 => 'quattro_b'];

$result = $array1 + $array2;

var_dump($result);

Le chiavi del primo array verranno preservate. Se una chiave esiste in entrambi gli array, verrà usato l'elemento del primo array e l'elemento con la chiave corrispondente del secondo array verrà ignorato.

array(5) {
  [0]=>
  string(6) "zero_a"
  [2]=>
  string(5) "due_a"
  [3]=>
  string(5) "tre_a"
  [1]=>
  string(5) "uno_b"
  [4]=>
  string(9) "quattro_b"
}

Vedere anche:

+add a note

User Contributed Notes 4 notes

up
322
Julian Egelstaff
17 years ago
In some situations, the union operator ( + ) might be more useful to you than array_merge.  The array_merge function does not preserve numeric key values.  If you need to preserve the numeric keys, then using + will do that.

ie:

<?php

$array1[0] = "zero";
$array1[1] = "one";

$array2[1] = "one";
$array2[2] = "two";
$array2[3] = "three";

$array3 = $array1 + $array2;

//This will result in::

$array3 = array(0=>"zero", 1=>"one", 2=>"two", 3=>"three");

?>

Note the implicit "array_unique" that gets applied as well.  In some situations where your numeric keys matter, this behaviour could be useful, and better than array_merge.

--Julian
up
60
ChrisM
4 years ago
I wished to point out that while other comments state that the spread operator should be faster than array_merge, I have actually found the opposite to be true for normal arrays. This is the case in both PHP 7.4 as well as PHP 8.0. The difference should be negligible for most applications, but I wanted to point this out for accuracy.

Below is the code used to test, along with the results:

<?php
$before = microtime(true);

for ($i=0 ; $i<10000000 ; $i++) {
    $array1 = ['apple','orange','banana'];
    $array2 = ['carrot','lettuce','broccoli'];
    
    $array1 = [...$array1,...$array2];
}

$after = microtime(true);
echo ($after-$before) . " sec for spread\n";

$before = microtime(true);

for ($i=0 ; $i<10000000 ; $i++) {
    $array1 = ['apple','orange','banana'];
    $array2 = ['carrot','lettuce','broccoli'];
    
    $array1 = array_merge($array1,$array2);
}

$after = microtime(true);
echo ($after-$before) . " sec for array_merge\n";
?>

PHP 7.4:
1.2135608196259 sec for spread
1.1402177810669 sec for array_merge

PHP 8.0:
1.1952061653137 sec for spread
1.099925994873 sec for array_merge
up
19
Andreas Hofmann
5 years ago
In addition to the text and Julian Egelstaffs comment regarding to keep the keys preserved with the + operator:
When they say "input arrays with numeric keys will be renumbered" they MEAN it. If you think you are smart and put your numbered keys into strings, this won't help. Strings which contain an integer will also be renumbered! I fell into this trap while merging two arrays with book ISBNs as keys. So let's have this example:

<?php
    $test1['24'] = 'Mary';
    $test1['17'] = 'John';

    $test2['67'] = 'Phil';
    $test2['33'] = 'Brandon';

    $result1 = array_merge($test1, $test2);
    var_dump($result1);

    $result2 = [...$test1, ...$test2];    // mentioned by fsb
    var_dump($result2);
?>

You will get both:

array(4) {
  [0]=>
  string(4) "Mary"
  [1]=>
  string(4) "John"
  [2]=>
  string(4) "Phil"
  [3]=>
  string(7) "Brandon"
}

Use the + operator or array_replace, this will preserve - somewhat - the keys:

<?php
    $result1 = array_replace($test1, $test2);
    var_dump($result1);

    $result2 = $test1 + $test2;
    var_dump($result2);
?>

You will get both:

array(4) {
  [24]=>
  string(4) "Mary"
  [17]=>
  string(4) "John"
  [67]=>
  string(4) "Phil"
  [33]=>
  string(7) "Brandon"
}

The keys will keep the same, the order will keep the same, but with a little caveat: The keys will be converted to integers.
up
3
fsb at thefsb dot org
6 years ago
We no longer need array_merge() as of PHP 7.4.

    [...$a, ...$b]

does the same as

    array_merge($a, $b)

and can be faster too.

https://wiki.php.net/rfc/spread_operator_for_array#advantages_over_array_merge
To Top