我想提取一个array_diff来比较两个多维数组的结构和键。
示例:
<?php
$array_1 =
['abc'=> '',
'def'=> ['', '', [
'ijk' => '']
]
];
$array_2 =
['abc'=> '',
'def'=> ['', '', [
'ijK' => '']
]
];
我想要一个这样的阵列差异告诉我关键的&#39; ijK&#39;是不是同一个名字&#39; ijk&#39; (或不存在于第一个阵列上)。
只有键才对我很重要......值总是空的。
答案 0 :(得分:0)
function array_key_recursive_compare($array1, $array2) {
$diff = false;
// Left-to-right
foreach ($array1 as $key => $value) {
if (!array_key_exists($key,$array2)) {
$diff[0][$key] = $value;
} elseif (is_array($value)) {
if (!is_array($array2[$key])) {
$diff[0][$key] = $value;
$diff[1][$key] = $array2[$key];
} else {
$new = array_compare($value, $array2[$key]);
if ($new !== false) {
if (isset($new[0]))
$diff[0][$key] = $new[0];
if (isset($new[1]))
$diff[1][$key] = $new[1];
};
};
} elseif ($array2[$key] !== $value) {
$diff[0][$key] = $value;
$diff[1][$key] = $array2[$key];
};
};
// Right-to-left
foreach ($array2 as $key => $value) {
if (!array_key_exists($key,$array1)) {
$diff[1][$key] = $value;
}
// No direct comparison because matching keys were compared in the
// left-to-right loop earlier, recursively.
}
return $diff;
}