gpt4 book ai didi

php - 多维数组差异

转载 作者:行者123 更新时间:2023-12-04 21:48:30 25 4
gpt4 key购买 nike

我有 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'
)
)
);

有什么建议吗?

最佳答案

是的,你可以使用下面的函数来获得完美的答案

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
)

)

)

检查 here了解更多信息

关于php - 多维数组差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16359322/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com