Array Operators

Array Operators
Example Name Result
$a + $b Union Union of $a and $b.
$a == $b Equality TRUE if $a and $b have the same key/value pairs.
$a === $b Identity TRUE if $a and $b have the same key/value pairs in the same order and of the same types.
$a != $b Inequality TRUE if $a is not equal to $b.
$a <> $b Inequality TRUE if $a is not equal to $b.
$a !== $b Non-identity TRUE if $a is not identical to $b.

The + operator returns the right-hand array appended to the left-hand array; for keys that exist in both arrays, the elements from the left-hand array will be used, and the matching elements from the right-hand array will be ignored.

<?php
$a 
= array("a" => "apple""b" => "banana");
$b = array("a" => "pear""b" => "strawberry""c" => "cherry");

$c $a $b// Union of $a and $b
echo "Union of \$a and \$b: \n";
var_dump($c);

$c $b $a// Union of $b and $a
echo "Union of \$b and \$a: \n";
var_dump($c);
?>
When executed, this script will print the following:
Union of $a and $b:
array(3) {
  ["a"]=>
  string(5) "apple"
  ["b"]=>
  string(6) "banana"
  ["c"]=>
  string(6) "cherry"
}
Union of $b and $a:
array(3) {
  ["a"]=>
  string(4) "pear"
  ["b"]=>
  string(10) "strawberry"
  ["c"]=>
  string(6) "cherry"
}

Elements of arrays are equal for the comparison if they have the same key and value.

Example #1 Comparing arrays

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

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

See also the manual sections on the Array type and Array functions.

Коментарии

[]= 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 )
2002-12-10 00:41:51
http://php5.kiev.ua/manual/ru/language.operators.array.html
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
2004-04-22 16:40:32
http://php5.kiev.ua/manual/ru/language.operators.array.html
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 (=> "apple"=> "banana");
$b = array (=> "banana"=> "apple");

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

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

var_dump($a === $b); // prints bool(true)
?>
2007-04-20 20:54:27
http://php5.kiev.ua/manual/ru/language.operators.array.html
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
)
2008-10-15 14:39:13
http://php5.kiev.ua/manual/ru/language.operators.array.html
Автор:
It should be mentioned that the array union operator functions almost identically to array_replace with the exception that precedence of arguments is reversed.
2012-03-04 20:39:01
http://php5.kiev.ua/manual/ru/language.operators.array.html
$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.
2017-09-21 11:02:43
http://php5.kiev.ua/manual/ru/language.operators.array.html
Автор:
Merge two arrays and retain only unique values.
Append values from second array.
Do not care about keys.

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

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

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

Result:
[
  [0] => "apple",
  [1] => "orange",
  [2] => "pear",
  [3] => "melon",
  [4] => "banana",
}
2022-10-19 12:32:42
http://php5.kiev.ua/manual/ru/language.operators.array.html

    Поддержать сайт на родительском проекте КГБ