//仅适用于纯关联数组
function deep_ksort(&$arr) {
ksort($arr);
foreach ($arr as &$a) {
if (is_array($a) && !empty($a)) {
deep_ksort($a);
}
}
}
deep_ksort($a);
deep_ksort($b);
if(json_encode($a) == json_encode($b)){
echo "两个数组相同";
}
//通用方法
//此函数将多维数组转化为一维数组
function multiToSingle($arr, $delimiter = '->',$key = ' ') {
$resultAry = array();
if (!(is_array($arr) && count($arr)>0)) {
return false;
}
foreach ($arr AS $k=>$val) {
$k = is_numeric($k) ? 0 :$k; //若键是数字,则统一为0,避免索引不同导致数组的不同
$newKey = trim($key . $k . $delimiter);
if (is_array($val) && count($val)>0) {
$resultAry = array_merge($resultAry, multiToSingle($val, $delimiter, $newKey));
} else {
$resultAry[] = $newKey . $val;
}
}
return $resultAry;
}
//判断两数组是否全等
function judge($a,$b)
{
$single_A = multiToSingle($a);
$single_B = multiToSingle($b);
$arr1 = array_diff($single_A,$single_B);
$arr2 = array_diff($single_B,$single_A);
if(empty($arr1) && empty($arr2)){
echo "两数组全等";
}else{
echo "不全等";
};
}