To solve the intersection problem of two arrays, you can use array_intersect (), array_inersect_assoc, and array_intersect_key. here, array_intersect () the function is used to calculate the intersection of two numbers and returns an array (only an array is worth comparing) of the common elements of an intersection. the array_intersect_assoc () function binds the key value and value to compare the intersection part and the array_intersect_key () A function is an array that compares the key values of two arrays and returns the intersection of key values.
However, some minor problems have also been encountered in practical applications, as shown below:
Instance:
The code is as follows:
$ Array = array ("red" => "Red", "green" => "red4", "Red15" => "Red", 7 => "Level ", "Width" => "Red", "azzzz1" => "art", "peak" => 158 );
$ Array1 = array ("red" => "Red2", "greena" => "red", "Red15" => "Red", 7 => "Level ", "Width" => "Red", "azzzz" => "art", "peak" => 158 );
$ Num = array_intersect ($ array, $ array1 );
Print_r ($ num );
Echo"
";
$ Num = array_intersect_assoc ($ array, $ array1 );
Print_r ($ num );
Echo"
";
$ Num = array_intersect_key ($ array, $ array1 );
Print_r ($ num );
?>
Running result:
The code is as follows:
Array ([red] => Red [Red15] => Red [7] => Level [Width] => Red [azzzz1] => art [peak] => 158)
Array ([Red15] => Red [7] => Level [Width] => Red [peak] => 158)
Array ([red] => Red [Red15] => Red [7] => Level [Width] => Red [peak] => 158)
Summary:
1. the array_intersect () function only compares the array values. if "Red" and "Red2" are compared, "Red" is returned. otherwise, "Red2" is not returned ";
2. the array_intersect_assoc () function compares the array value with the key value without the case of array_intersect. This function is suitable for strict comparison;
3. the array_intersect_key () function is applicable to comparing the intersection of two array key values. instead of only the key value, it returns the key value and the corresponding array value.