Lecture
Here is a list of operators working with PHP arrays:
Example | Title | Result |
---|---|---|
$ a + $ b | Union | Combining array $ a and array $ b. |
$ a == $ b | Equally | TRUE if $ a and $ b contain the same elements. |
$ a === $ b | Identically equal | TRUE if $ a and $ b contain the same elements in the same order. |
$ a! = $ b | Not equal | TRUE if array $ a is not equal to array $ b. |
$ a <> $ b | Not equal | TRUE if array $ a is not equal to array $ b. |
$ a! == $ b | Identically not equal | TRUE if the array $ a is not equal identically to the array $ b. |
The + operator appends the right-hand array to the array located on the left NOT overwriting items with duplicate keys.
<?php
$a = array("a" => "apple", "b" => "banana");
$b = array("a" => "pear", "b" => "strawberry", "c" => "cherry");
$c = $a + $b; // Объеденение $a и $b
echo "Union of \$a and \$b: \n";
var_dump($c);
$c = $b + $a; // Объединение $b и $a
echo "Union of \$b and \$a: \n";
var_dump($c);
?>
After its execution, the script displays 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"
}
When comparing, the elements of an array are considered identical if both the key and the corresponding value match.
<?php
$a = array("apple", "banana");
$b = array(1 => "banana", "0" => "apple");
var_dump($a == $b); // bool(true)
var_dump($a === $b); // bool(false)
?>
See also: Arrays and Array Functions.
Comments
To leave a comment
Running server side scripts using PHP as an example (LAMP)
Terms: Running server side scripts using PHP as an example (LAMP)