Learn a new php function every day (2) array_diff ()/array_diff_key ()/array_diff_assoc ()
Array_diff (array array1, array array2 [, array…] )
Description
Array_diff () returns an array containing all values in the array of array1 but not in any other parameter. Note that the key name remains unchanged.
Column
"green", "red", "blue", "red"); $array2 = array("b" => "green", "yellow", "red"); $result = array_diff($array1, $array2); print_r($result);?>
The output result is
Array([1] => blue)
Note:
The two units are considered the same only when (string) $ elem1 === (string) $ elem2. That is to say, when the expression of the string is the same. Note that this function only checks one dimension in a multi-dimensional array. Of course, you can use array_diff ($ array1 [0], $ array2 [0]); to check deeper dimensions.
Array array_diff_assoc (array array1, array array2 [, array…] )
Description
Array_diff_assoc () returns an array containing all values in the array of array1 but not in any other parameter. Note that, unlike array_diff (), the key name is also used for comparison.
Column
"green", "b" => "brown", "c" => "blue", "red"); $array2 = array("a" => "green", "yellow", "red"); $result = array_diff_assoc($array1, $array2); print_r($result);?>
Output
Array( [b] => brown [c] => blue [0] => red)
In the above example, we can see that the key-Value Pair "a" => "green" exists in both arrays, so it is not in the output of this function. Different from this, the key-Value Pair 0 => "red" appears in the output because the key name of "red" in the second parameter is 1.
Column 2
Output
Array( [0] => 0 [1] => 1)
Key-value Pair key => the two values in value are only in (string) Elem1 = (string) Elem2 is considered equal. That is to say, the use of strict check, the expression of the string must be the same.
Note:
Note: This function only checks one dimension in a multi-dimensional array. Of course, you can use array_diff_assoc ($ array1 [0], $ array2 [0]); to check deeper dimensions.
Array array_diff_key (array $ array1, array $ array2 [, array $... ])
Description
Compare the key names in array1 with those in array2. the items with different key names are returned. This function is the same as array_diff () except that the comparison is based on the key name rather than the value.
Returns an array containing all the key names that appear in array1 but not present in any other parameter array.
Column
1, 'red' => 2, 'green' => 3, 'purple' => 4); $array2 = array('green' => 5, 'blue' => 6, 'yellow' => 7, 'cyan' => 8); var_dump(array_diff_key($array1, $array2));?>
Output
array(2) { ["red"]=> int(2) ["purple"]=> int(4)}
The two key names in the key => value pair are considered equal only when (string) $ key1 === (string) $ key2. In other words, strict type check is executed, so the expression of strings must be exactly the same.
Array array_intersect (array $ array1, array $ array2 [, array $... ])
Description
Array_intersect () returns an array containing all values that appear in the array of all other parameters at the same time in array1. Note that the key name remains unchanged.