我有2个阵列:
$big_array = array (
array (
array (
'correct' => FALSE,
'answer' => 'false answer1'
),
array (
'correct' => FALSE,
'answer' => 'false answer2'
),
array (
'correct' => FALSE,
'answer' => 'false answer3'
),
array (
'correct' => TRUE,
'answer' => 'correct answer'
)
)
);
$small_array = array (
array (
array (
'correct' => FALSE,
'answer' => 'false answer1'
)
)
);
我想制作array_diff($big_array, $small_array)
,但它的工作原理并不是我想要的,目前它给了我一个空数组,我希望结果如下:
$outcome_array= array (
array (
array (
'correct' => FALSE,
'answer' => 'false answer2'
),
array (
'correct' => FALSE,
'answer' => 'false answer3'
),
array (
'correct' => TRUE,
'answer' => 'correct answer'
)
)
);
有什么建议吗?
答案 0 :(得分:11)
是的,您可以使用以下功能来获得完美答案
function arrayRecursiveDiff($aArray1, $aArray2) {
$aReturn = array();
foreach ($aArray1 as $mKey => $mValue) {
if (array_key_exists($mKey, $aArray2)) {
if (is_array($mValue)) {
$aRecursiveDiff = arrayRecursiveDiff($mValue, $aArray2[$mKey]);
if (count($aRecursiveDiff)) { $aReturn[$mKey] = $aRecursiveDiff; }
} else {
if ($mValue != $aArray2[$mKey]) {
$aReturn[$mKey] = $mValue;
}
}
} else {
$aReturn[$mKey] = $mValue;
}
}
return $aReturn;
}
$arr1 = arrayRecursiveDiff($big_array,$small_array);
<强>输出强>
根据需要
Array
(
[0] => Array
(
[1] => Array
(
[correct] =>
[answer] => false answer2
)
[2] => Array
(
[correct] =>
[answer] => false answer3
)
[3] => Array
(
[correct] => 1
[answer] => correct answer
)
)
)
答案 1 :(得分:0)
编辑上面的代码检查arr1是否更大或arr2是否更大
function arrayRecursiveDiff($aArray1, $aArray2) {
if(sizeof($aArray1) > sizeof($aArray2))
{
$small_arr = $aArray2;
$big_arr = $aArray1;
$arr1_is_big = 1;
}
else
{
$small_arr= $aArray1;
$big_arr = $aArray2;
$arr1_is_big = 0;
}
$aReturn = array();
foreach ($big_arr as $mKey => $mValue) {
if (array_key_exists($mKey, $small_arr)) {
if (is_array($mValue)) {
$aRecursiveDiff = arrayRecursiveDiff($mValue, $small_arr[$mKey]);
if (count($aRecursiveDiff)) { $aReturn[$mKey] = $aRecursiveDiff; }
}
else {
if ($mValue != $small_arr[$mKey]) {
$aReturn[$mKey] = $mValue;
}
}
} else {
$aReturn[$mKey] = $mValue;
}
}
$aReturn["arr1_is_big"] = $arr1_is_big;
return ($aReturn);
}
答案 2 :(得分:0)
我刚刚编辑了Murali Kumar的功能,因为我还需要一个信息。 它将检查数组是否大小相同并包含相同的值。
function arrayRecursiveDiff($aArray1, $aArray2) {
if(count($aArray1) > count($aArray2)) {
$small_arr = $aArray2;
$big_arr = $aArray1;
$arr1_is_big = 1;
$arr2_is_big = 0;
} elseif(count($aArray1) < count($aArray2)) {
$small_arr = $aArray1;
$big_arr = $aArray2;
$arr1_is_big = 0;
$arr2_is_big = 1;
} else {
$small_arr = $aArray1;
$big_arr = $aArray2;
$arr1_is_big = 0;
$arr2_is_big = 0;
}
$aReturn = array();
foreach($big_arr as $mKey => $mValue) {
if(array_key_exists($mKey, $small_arr)) {
if(is_array($mValue)) {
$aRecursiveDiff = arrayRecursiveDiff($mValue, $small_arr[$mKey]);
if(count($aRecursiveDiff)) {
$aReturn[$mKey] = $aRecursiveDiff;
}
} else {
if($mValue != $small_arr[$mKey]) {
$aReturn[$mKey] = $mValue;
}
}
} else {
$aReturn[$mKey] = $mValue;
}
}
if($arr1_is_big)
$aReturn["arr1_is_big"] = $arr1_is_big;
if($arr2_is_big)
$aReturn["arr2_is_big"] = $arr2_is_big;
return ($aReturn);
}