我下面有两个多维数组。
Array
(
[ma] => Array
(
[mdesktopapps] => Array
(
[0] => part1
[1] => part2
)
)
)
Array
(
[ma] => Array
(
[mdesktopapps] => Array
(
[0] => part2
)
)
)
我想得到上面的数组之间的区别,输出应该像下面这样
Array
(
[ma] => Array
(
[mdesktopapps] => Array
(
[0] => part1
)
)
)
我尝试使用以下功能获取输出,但未获取正确的输出。
function array_diff_assoc_recursive($array1, $array2) {
$difference=array();
foreach($array1 as $key => $value) {
if( is_array($value) ) {
if( !isset($array2[$key]) || !is_array($array2[$key]) ) {
$difference[$key] = $value;
} else {
$new_diff = array_diff_assoc_recursive($value, $array2[$key]);
if( !empty($new_diff) )
$difference[$key] = $new_diff;
}
} else if( !array_key_exists($key,$array2) || $array2[$key] !== $value ) {
$difference[] = $value;
}
}
return $difference;
}
Array
(
[ma] => Array
(
[mdesktopapps] => Array
(
[0] => part1
[1] => part2
)
)
)
您的问题来自您在else条件中放入的逻辑:
...
} else if( !array_key_exists($key,$array2) || $array2[$key] !== $value ) {
...
现在,您要做的是,第二次调用该方法,并传递“ ma”数组,然后开始迭代值“ part1”和“ part2”,如果第一个值是“ part1”,则进入其他方法“您需要满足条件$ array2 [$ key]!== $ value,因为在$ array2 [$ key]($ array2 [0])中,值是“ part2”,而在第一次迭代中,$ value是“ part1”,所以您可以将此值分配给$ difference []。到目前为止一切顺利,但是在第二次迭代中,值实际上是“ part2”,您实际上满足了此条件-!array_key_exists($ key,$ array2),因为您的$ array2没有键1,所以您将其添加到$ difference [ ]此结果也可以...您可以做什么:
// now i've added 3rd argument to the function called recursiveCount
// by default is false so this count happens only if you pass true as 3rd arg
function array_diff_assoc_recursive($a, $b, $recursiveCount = false) {
// by default we set $array1 to be $a and $array2 to be $b
$array1 = $a;
$array2 = $b;
// but if we "put the count flag" and if the first array we pass
// is smaller that the 2nd we will reverse it
// so you don't have to worry about if the 1st array you pass in the function is smaller than the 2nd
if ($recursiveCount && (count($array1, COUNT_RECURSIVE) < count($array2, COUNT_RECURSIVE))) {
$array1 = $b;
$array2 = $a;
}
$difference = [];
foreach ($array1 as $key => $value) {
if (is_array($value)) {
if (!isset($array2[$key]) || !is_array($array2[$key])) {
$difference[$key] = $value;
} else {
// now note that now when we call the function here we dont pass the 3rd argument, to count our arrays
$newDiff = $this->array_diff_assoc_recursive($value, $array2[$key]);
if (!empty($newDiff)) {
$difference[$key] = $newDiff;
}
}
} else {
// also 1 small change here just in case arr2 is not an array actually
if (is_array($array2) && !in_array($value, $array2)) {
$difference[] = $value;
} elseif (!is_array($array2) && $value != $array2) {
$difference[] = $value;
}
}
}
return $difference;
}
现在,如何在第三个参数中使用它:
$aa = [
"ma" => [
"mdesktopapps" => ["part1","part2","part3","part4","part5"]
]
];
$ab = [
"ma" => [
"mdesktopapps" => ["part2","part5","part4"]
]
];
// note we pass true as 3rd arg, so we will count our multidimensional arrays
// and because $ab < $aa they will be switched in out function
$t = $this->array_diff_assoc_recursive($ab, $aa, true);
var_dump($t);
结果:
array(1) {
'ma' =>
array(1) {
'mdesktopapps' =>
array(2) {
[0] =>
string(5) "part1"
[1] =>
string(5) "part3"
}
}
}
就是这样,从这里您可以使用它并对其进行更多升级...
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句