Detailed explanation of PHP array operator: little-known tips
PHP operators can be divided into seven categories: arithmetic, assignment, bit operation, comparison, error control, execution, increment/decrease, logic, string, array and type operator. This article focuses on array operators and covers the behavior of some other operators when used in conjunction with arrays.
The official documentation only briefly describes each array operator, which sometimes makes it difficult for people to understand the expected results of each operator. Let's take a closer look at each array operator to get a clearer understanding of their functionality. All of these operators are binary, which means that each operator acts precisely on two arrays.
First is the union operator ( ), which gives the union of two arrays according to the keys of the array. It performs loose key matching, and if the equivalent key of the second array already exists in the first array, all keys of the second array are ignored. The remaining keys of the second array (and the corresponding values) are appended to the first array.
<?php $array1 = array('a', 'b', 'c'); $array2 = array('d', 'e', 'f', 'g', 'h', 'i'); print_r($array1 + $array2); print_r($array2 + $array1); ?>
<code>Array ( [0] => a [1] => b [2] => c [3] => g [4] => h [5] => i ) Array ( [0] => d [1] => e [2] => f [3] => g [4] => h [5] => i )</code>
For the first print_r(), the first three elements in $array2 have keys that already exist in $array1, so 'd', 'e' and 'f' are ignored in the result array. For the second print_r(), all keys of $array1 already exist in $array2, so all its elements are ignored. Loose matching behavior may give you totally unexpected results, but also provides exciting opportunities for optimization and loose coding.
<?php $array1 = array('0' => 'a', '1' => 'b', '2' => 'c', '3' => 'd'); $array2 = array(false => 'e', 1 => 'f', 2 => 'g', 3 => 'h', 4 => 'i'); print_r($array1 + $array2); ?>
<code>Array ( [0] => a [1] => b [2] => c [3] => d [4] => i )</code>
People often misunderstand that unions are based on array values, but in fact this operator implements unions of array keys. For value-based union, you can use array_merge() and array_unique() in combination:
<?php $array1 = array('a', 'b', 'c'); $array2 = array('d', 'e', 'f', 'g', 'h', 'i'); print_r($array1 + $array2); print_r($array2 + $array1); ?>
<code>Array ( [0] => a [1] => b [2] => c [3] => g [4] => h [5] => i ) Array ( [0] => d [1] => e [2] => f [3] => g [4] => h [5] => i )</code>
Equality operator (==) checks whether the two arrays are similar. If all key-value pairs in the first array have equivalent key-value pairs in the second array, the operator returns true. It loosely matches values and keys and ignores the order of elements.
<?php $array1 = array('0' => 'a', '1' => 'b', '2' => 'c', '3' => 'd'); $array2 = array(false => 'e', 1 => 'f', 2 => 'g', 3 => 'h', 4 => 'i'); print_r($array1 + $array2); ?>
<code>Array ( [0] => a [1] => b [2] => c [3] => d [4] => i )</code>
The elements in both arrays are in different order, but the same values are bound to similar keys in each array. However, the following two are not equal, because both arrays have different key-value pairs:
<?php $union = array_unique(array_merge($array1, $array2)); print_r($union); ?>
<code>Array ( [0] => a [1] => b [2] => c [3] => d [4] => e [5] => f [6] => g [7] => h [8] => i )</code>
The unequal operator (!= or <>) checks whether the two arrays are not similar and are perfect antonyms for the equal operator. The equality operator returns anything that false, this operator returns true and vice versa.
<?php $array1 = array('1' => 1, '2' => 2, '3' => 3, '0' => 0); $array2 = array(false => '0', 1 => '1', 2 => '2', 3 => '3'); var_dump($array1 == $array2); ?>
<code>bool(true)</code>
Identity operator (===) checks whether the two arrays are the same. Two arrays are the same if they meet the following conditions:
However, for array keys, if the key is an integer, and there is a similar integer string representation as a key in another array, the identity operator makes a loose match. This operator will strictly match the floating point number to the string key. The PHP manual does not state this difference.
<?php $array1 = array(1, 2); $array2 = array(2, 1); var_dump($array1 == $array2); ?>
<code>bool(false)</code>
<?php $array1 = array('1' => 1, '2' => 2, '3' => 3, '0' => 0); $array2 = array(false => '0', 1 => '1', 2 => '2', 3 => '3'); var_dump($array1 != $array2); ?>
<code>bool(false)</code>
<?php // 数组几乎相同,但键的类型不同 $array1 = array('0' => '0', '1' => '1', '2' => '2', '3' => '3'); $array2 = array(0 => '0', 1 => '1', 2 => '2', 3 => '3'); var_dump($array1 === $array2); ?>
<code>bool(true)</code>
The non-identity operator (!==) checks whether the two arrays are different. Again, this operator is exactly the opposite of the identity operator, which means that if the two arrays are the same, this operator returns false.
<?php // 两个数组中的元素顺序不同 $array1 = array('0' => '0', '1' => '1', '2' => '2', '3' => '3'); $array2 = array(1 => '1', 2 => '2', 3 => '3', 0 => '0'); var_dump($array1 === $array2); ?>
<code>bool(false)</code>
PHP behavior differs when applying operators other than the above operators to an array. Here is a list of these operators and how they behave when applied to an array.
PHP will issue a fatal error when the following operator is applied to the array:
When used with the following operators, an array is treated as an integer. An empty array (no elements) is considered int(0), and a non-empty array is considered int(1).
When concatenating two arrays, the string concatenation operator (.) treats each array as a string "Array" and concatenates these strings.
<?php $array1 = array('a', 'b', 'c'); $array2 = array('d', 'e', 'f', 'g', 'h', 'i'); print_r($array1 + $array2); print_r($array2 + $array1); ?>
<code>Array ( [0] => a [1] => b [2] => c [3] => g [4] => h [5] => i ) Array ( [0] => d [1] => e [2] => f [3] => g [4] => h [5] => i )</code>
Increment/decrement operators (and --) have no effect on the array.
<?php $array1 = array('0' => 'a', '1' => 'b', '2' => 'c', '3' => 'd'); $array2 = array(false => 'e', 1 => 'f', 2 => 'g', 3 => 'h', 4 => 'i'); print_r($array1 + $array2); ?>
<code>Array ( [0] => a [1] => b [2] => c [3] => d [4] => i )</code>
There is little actual documentation about PHP operators when used with arrays, but to learn more, you can view user-submitted comments on the Array Operators page. Your questions and comments are welcome here and I will be happy to explain further.
Pictures from Fotolia
PHP supports several types of array operators, including union ( ), equality (==), identity (===), inequality (!= or <>) and non-constant Equality (!==). Each of these operators performs a different function. For example, the union operator ( ) combines an array, the equality operator (==) checks whether the two arrays are equal, and the identity operator (===) checks whether the two arrays are the same.
The union ( ) operator in PHP combines two numbers into one. It takes a union of arrays, which means it returns an array containing all elements in two arrays. If the array has the same string key, the value from the first array will be used, and the value of the matching key in the second array will be ignored.
Equality (==) operator checks whether two arrays have the same key/value pairs, regardless of their order or data type. On the other hand, the identity (===) operator checks whether two arrays have the same key/value pairs of the same order and the same data type.
The unequal operator in PHP is represented by != or <> to check whether the two arrays are not equal. Return true if the array is not equal, and false if the array is equal.
The non-identity (!==) operator in PHP checks whether the two arrays are different. Return true if the array is not the same; false if the array is the same.
Yes, you can combine array operators in PHP to perform more complex operations. However, be careful when doing this to avoid unexpected results. Always ensure that the combined operators have logical significance in the context of the code.
You can use the in_array() function in PHP to check if an array contains a specific value. If the value is found in the array, this function returns true; otherwise, it returns false.
You can use the array_diff() function in PHP to delete a specific value from an array. This function compares the values in the array with the values in another array and returns the difference.
PHP provides multiple functions to sort arrays, including sort(), asort(), ksort(), and usort(). Each of these functions sorts the array in a different way, so you should choose the one that best suits your needs.
You can use the array_reverse() function in PHP to invert the order of the array. This function returns a new array of elements in reverse order.
The above is the detailed content of PHP Master | Array Operators in PHP: Interesting but Less Spoken. For more information, please follow other related articles on the PHP Chinese website!