PHP function introduction—array_diff(): Compare the difference between two arrays
In the development of PHP, it is often necessary to operate and compare arrays. PHP provides many convenient functions to implement these operations. One of the commonly used functions is array_diff(), which helps us compare the difference between two arrays.
The array_diff() function is to delete the values that appear in other arrays from the first array and return a new array composed of the remaining values. This function accepts multiple arrays as parameters. We can compare the differences between arrays by passing the arrays that need to be compared as parameters to the array_diff() function.
Let’s look at a specific code example:
<?php $array1 = array("apple", "banana", "orange", "pear"); $array2 = array("apple", "banana", "grape"); $array3 = array("orange", "pear", "grapefruit"); $result = array_diff($array1, $array2, $array3); print_r($result); ?>
In the above code, we define three arrays $array1, $array2 and $array3, and then use these three arrays as parameters Passed to the array_diff() function. After running the code, the result will be that the values in $array1 are different from those in $array2 and $array3.
The output result is as follows:
Array ( [0] => orange [1] => pear )
You can see that the output result is a new array, which contains the values in $array1 that are different from $array2 and $array3" orange" and "pear".
It should be noted that the array_diff() function will only return values that appear in the first array but do not appear in all other arrays. If a value occurs in other arrays, it will not be included in the resulting array.
In addition, the array_diff() function can also be used for comparison of associative arrays. It ignores the key names in the array and only compares the differences between key values. For example:
<?php $array1 = array("apple" => 1, "banana" => 2, "orange" => 3, "pear" => 4); $array2 = array("apple" => 1, "banana" => 2, "grape" => 3); $array3 = array("orange" => 1, "pear" => 2, "grapefruit" => 3); $result = array_diff($array1, $array2, $array3); print_r($result); ?>
After running the above code, the output result is still:
Array ( [orange] => 3 [pear] => 4 )
It can be seen that when the key names are the same, the array_diff() function will compare the differences between the key values and Return different key values.
To sum up, the array_diff() function is a very practical array comparison function in PHP. Through it, we can easily compare the differences between two arrays and compare multiple arrays at the same time. difference. This is very useful when performing array operations and data processing, and can improve development efficiency. By flexibly using the array_diff() function, we can process data in the array more easily.
The above is the detailed content of PHP function introduction—array_diff(): Compare the difference between two arrays. For more information, please follow other related articles on the PHP Chinese website!