PHP 8.1.31 Released!

Opérateurs de tableaux

Opérateurs de tableaux
Exemple Nom Résultat
$a + $b Union Union de $a et $b.
$a == $b Égalité true si $a et $b contiennent les mêmes paires clés/valeurs.
$a === $b Identique true si $a et $b contiennent les mêmes paires clés/valeurs dans le même ordre et du même type.
$a != $b Inégalité true si $a n'est pas égal à $b.
$a <> $b Inégalité true si $a n'est pas égal à $b.
$a !== $b Non-identique true si $a n'est pas identique à $b.

L'opérateur + retourne le tableau de gauche auquel sont ajoutés les éléments du tableau de droite. Pour les clés présentes dans les 2 tableaux, les éléments du tableau de gauche seront utilisés alors que les éléments correspondants dans le tableau de droite seront ignorés.

<?php
$a
= array("a" => "pomme", "b" => "banane");
$b = array("a" =>"poire", "b" => "fraise", "c" => "cerise");

$c = $a + $b; // Union de $a et $b
echo "Union de \$a et \$b : \n";
var_dump($c);

$c = $b + $a; // Union de $b et $a
echo "Union de \$b et \$a : \n";
var_dump($c);

$a += $b; // Union de $a += $b est $a and $b
echo "Union de \$a += \$b: \n";
var_dump($a);
?>
À l'exécution, le script affichera :
Union de $a et $b :
array(3) {
  ["a"]=>
  string(5) "pomme"
  ["b"]=>
  string(6) "banane"
  ["c"]=>
  string(6) "cerise"
}
Union de $b et $a :
array(3) {
  ["a"]=>
  string(5) "poire"
  ["b"]=>
  string(6) "fraise"
  ["c"]=>
  string(6) "cerise"
}
Union de $a += $b:
array(3) {
  ["a"]=>
  string(5) "pomme"
  ["b"]=>
  string(6) "banane"
  ["c"]=>
  string(6) "cerise"
}

Les éléments d'un tableau sont égaux en termes de comparaison s'ils ont la même clé et la même valeur.

Exemple #1 Comparer des tableaux

<?php
$a
= array("pomme", "banane");
$b = array(1 => "banane", "0" => "pomme");

var_dump($a == $b); // bool(true)
var_dump($a === $b); // bool(false)
?>

add a note

User Contributed Notes 7 notes

up
238
cb at netalyst dot com
16 years ago
The union operator did not behave as I thought it would on first glance. It implements a union (of sorts) based on the keys of the array, not on the values.

For instance:
<?php
$a
= array('one','two');
$b=array('three','four','five');

//not a union of arrays' values
echo '$a + $b : ';
print_r ($a + $b);

//a union of arrays' values
echo "array_unique(array_merge($a,$b)):";
// cribbed from http://oreilly.com/catalog/progphp/chapter/ch05.html
print_r (array_unique(array_merge($a,$b)));
?>

//output

$a + $b : Array
(
[0] => one
[1] => two
[2] => five
)
array_unique(array_merge(Array,Array)):Array
(
[0] => one
[1] => two
[2] => three
[3] => four
[4] => five
)
up
42
Q1712 at online dot ms
17 years ago
The example may get u into thinking that the identical operator returns true because the key of apple is a string but that is not the case, cause if a string array key is the standart representation of a integer it's gets a numeral key automaticly.

The identical operator just requires that the keys are in the same order in both arrays:

<?php
$a
= array (0 => "apple", 1 => "banana");
$b = array (1 => "banana", 0 => "apple");

var_dump($a === $b); // prints bool(false) as well

$b = array ("0" => "apple", "1" => "banana");

var_dump($a === $b); // prints bool(true)
?>
up
25
dfranklin at fen dot com
20 years ago
Note that + will not renumber numeric array keys. If you have two numeric arrays, and their indices overlap, + will use the first array's values for each numeric key, adding the 2nd array's values only where the first doesn't already have a value for that index. Example:

$a = array('red', 'orange');
$b = array('yellow', 'green', 'blue');
$both = $a + $b;
var_dump($both);

Produces the output:

array(3) { [0]=> string(3) "red" [1]=> string(6) "orange" [2]=> string(4) "blue" }

To get a 5-element array, use array_merge.

Dan
up
7
xtpeqii at Hotmail dot com
7 years ago
$a=[ 3, 2, 1];
$b=[ 6, 5, 4];
var_dump( $a + $b );

output:
array(3) {
[0]=>
int(3)
[1]=>
int(2)
[2]=>
int(1)
}

The reason for the above output is that EVERY array in PHP is an associative one.
Since the 3 elements in $b have the same keys( or numeric indices ) as those in $a, those elements in $b are ignored by the union operator.
up
19
amirlaher AT yahoo DOT co SPOT uk
21 years ago
[]= could be considered an Array Operator (in the same way that .= is a String Operator).
[]= pushes an element onto the end of an array, similar to array_push:
<?
$array= array(0=>"Amir",1=>"needs");
$array[]= "job";
print_r($array);
?>
Prints: Array ( [0] => Amir [1] => needs [2] => job )
up
13
Dan Patrick
12 years ago
It should be mentioned that the array union operator functions almost identically to array_replace with the exception that precedence of arguments is reversed.
up
1
Anonymous
2 years ago
Merge two arrays and retain only unique values.
Append values from second array.
Do not care about keys.

<?php
$array1
= [
0 => 'apple',
1 => 'orange',
2 => 'pear',
];

$array2 = [
0 => 'melon',
1 => 'orange',
2 => 'banana',
];

$result = array_keys(
array_flip($array1) + array_flip($array2)
);
?>

Result:
[
[0] => "apple",
[1] => "orange",
[2] => "pear",
[3] => "melon",
[4] => "banana",
}
To Top