The example is as follows:
The actual operation is a three-dimensional array, and the amount of data is relatively large.
$a = [
['name'=>'jack', 'gender'=>'male'],
['age'=>18,'country'=>'China']
];
$b = [
['country'=>'China','age'=>18],
['gender'=>'male','name'=>'jack']
];
In addition to traversing and comparing, is there any quick and effective way?
My current idea is to sort the array according to the same rules first, and then use md5 verification after json_encoding. However, it seems that sorting cannot be implemented well at present.
I wonder if you have any good methods?
//Only applicable to purely associative arrays
function deep_ksort(&$arr) {
}
deep_ksort($a);
deep_ksort($b);
if(json_encode($a) == json_encode($b)){
}
//General method
//This function converts a multi-dimensional array into a one-dimensional array
function multiToSingle($arr, $delimiter = '->',$key = ' ') {
}
//Judge whether two arrays are congruent
function judge($a,$b)
{
if(empty($arr1) && empty($arr2)){
}else{
};
}
You can take a look at this http://bbs.csdn.net/topics/36..., it should be useful to you!
It’s very simple. After sorting by the same rules, convert the array into a string and compare whether the two strings are the same