PHP is a very popular and practical programming language that provides various methods for processing and operating arrays. During the development process, we often need to compare the difference between two arrays and display the different values. Below I will introduce some implementation methods.
Implementation method 1: Use the array_diff() function
The array_diff() function can compare the values of two arrays and return different values.
$array1 = array("a" => "red", "b" => "green", "c" => "blue"); $array2 = array("a" => "red", "b" => "blue", "d" => "yellow"); $result = array_diff($array1, $array2); print_r($result);
Run the above code, the output result is:
Array ( [b] => green [c] => blue )
As can be seen from the above results, the values of the keys "b" and "c" in $array1 and $array2 are different, so array_diff () function returns these values.
Implementation method 2: Use foreach loop
In addition to using the array_diff() function, we can also use foreach loop to compare the values of two arrays and display the different values.
$array1 = array("a" => "red", "b" => "green", "c" => "blue"); $array2 = array("a" => "red", "b" => "blue", "d" => "yellow"); foreach($array1 as $key => $value){ if($value != $array2[$key]){ echo "键为".$key."的值不同,分别为".$value."和".$array2[$key]."<br>"; } }
Run the above code, the output result is:
键为b的值不同,分别为green和blue 键为c的值不同,分别为blue和
As can be seen from the above results, the values of the keys "b" and "c" in $array1 and $array2 are different, so foreach The loop outputs these values.
It should be noted that when using a foreach loop to compare two arrays, you need to ensure that the two arrays have the same keys, otherwise a Notice: Undefined offset error may occur.
Implementation method 3: Use array_map() function
Another way to compare the values of two arrays is to use the array_map() function. This function will apply a custom function to the values of the same key in both arrays, saving the results in a new array.
$array1 = array("a" => "red", "b" => "green", "c" => "blue"); $array2 = array("a" => "red", "b" => "blue", "d" => "yellow"); $result = array_map(function($a, $b){ if($a != $b){ return array($a, $b); } }, $array1, $array2); print_r($result);
Run the above code, the output result is:
Array ( [b] => Array ( [0] => green [1] => blue ) [c] => Array ( [0] => blue [1] => ) [d] => Array ( [0] => [1] => yellow ) )
As can be seen from the above results, the values of the keys "b" and "c" in $array1 and $array2 are different, so array_map () function returns these values.
It should be noted that when using the array_map() function to compare two arrays, you need to use a custom function to implement the comparison logic.
In summary, there are many implementation methods for comparing the values of two arrays and displaying different values. Among them, the array_diff() function and the foreach loop are relatively simple and clear, and are suitable for relatively small-scale arrays; while the array_map() function is more suitable for relatively large-scale arrays, and custom functions can be used to implement more complex comparison logic. In actual development, selecting appropriate methods for comparison based on the actual situation can improve development efficiency and code quality.
The above is the detailed content of How to compare two arrays with different values in php. For more information, please follow other related articles on the PHP Chinese website!