Home > Article > Daily Programming > How to compare two PHP multidimensional arrays
# Determine whether two multi-dimensional arrays are equal, or what elements are different. We can achieve comparison through array_udiff() function, create_function() method, strcmp () and other functions in PHP.
Now we will introduce the method of comparing two two-dimensional arrays with specific code examples.
The code example is as follows:
<?php //多维数组比较 $color1 = array(array('Red',80),array('Green',70),array('white',60)); $color2 = array(array('Green',70),array('Black',95)); $color = array_udiff($color1,$color2,create_function( '$a,$b','return strcmp(implode("",$a),implode("",$b));') ); echo "<pre class="brush:php;toolbar:false">"; print_r($color);
In this code, we can get the different array elements of the $color1 array compared to the $color2 array.
The output comparison results are as follows:
Function introduction:
1. array_udiff: Use the callback function to compare the data to calculate the difference set of the array
array_udiff ( array $array1 , array $array2 [, array $... ], callable $value_compare_func ) : array
Use the callback function to compare the data and calculate the difference between the arrays. Unlike array_diff(), the former uses built-in functions for data comparison.
Parameter array1 represents the first array. array2 second array. value_compare_func callback comparison function.
When the first argument is less than, equal to, or greater than the second argument, the comparison function must return an integer that is less than, equal to, or greater than 0 accordingly.
callback (mixed $a, mixed $b): int
Return value, returns all values in array1 that do not appear in other parameters.
2, create_function: Create an anonymous function
create_function ( string $args , string $code ): string
Create an anonymous function based on the passed parameters and assign it to Returns a unique name.
Normally these parameters will be passed as single quote delimited strings. The reason for using single quoted strings is to protect the variable name from being parsed, otherwise if you use double quotes, you would need to escape the variable name, such as \$avar.
args represents function parameters. code represents function code.
The return value is a unique function name returned in the form of a string or FALSE error.
3. strcmp: Binary safe string comparison
strcmp ( string $str1 , string $str2 ) : int
Note that this comparison is case-sensitive.
The parameter str1 represents the first string. str2 represents the second string.
The return value is if str1 is less than str2, return 0; if they are equal, return 0.
4. implode: Convert the value of a one-dimensional array into a string
This article is about the method of comparing two PHP multi-dimensional arrays, and it is also very useful. Simple and easy to understand, I hope it will be helpful to friends in need!
The above is the detailed content of How to compare two PHP multidimensional arrays. For more information, please follow other related articles on the PHP Chinese website!