示例如下:
实际操作的是三维数组,而且数据量比较大。
$a = [ ['name'=>'jack', 'gender'=>'male'], ['age'=>18,'country'=>'China'] ]; $b = [ ['country'=>'China','age'=>18], ['gender'=>'male','name'=>'jack'] ];
除了遍历比较外有什么快捷有效的方式?
我现在的想法是先把数组按相同规则排序,json_encode后使用md5校验。不过目前似乎并不能很好的实现排序。
不知各位有什么很好的方法?
//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