我想在键值相同时找到两个多维数组之间的差异。 我已经尝试了所有的东西和很多Stackoverflow解决方案,但我无法做到。
数组1(DbData): -
$ajaxData = array("phase-5[]" =>
array(
'1' => "Admin Phase 1 Task 1",
'2' => "Admin Phase 1 Task 2"
),"phase-6[]" =>
array(
'1' => "Admin Phase 2 Task 1",
'2' => "Admin Phase 2 Task 2",
'3' => "Admin Phase 2 Task 3"
),"phase-7[]" =>
array(
'1' => "Admin Phase 3 Task 1",
'2' => "Admin Phase 3 Task 2",
'3' => "Admin Phase 3 Task 3"
)
);
数组2(AjaxData): -
$dbData = array("phase-5[]" =>
array(
'0' => "Admin Phase 1 Task 1",
'1' => "Admin Phase 1 Task 2"
),"phase-6[]" =>
array(
'0' => "Admin Phase 2 Task 1",
'1' => "Admin Phase 2 Task 2"
),"phase-7[]" =>
array(
'0' => "Admin Phase 3 Task 1",
'1' => "Admin Phase 3 Task 2"
)
);
PHP代码: -
$ajaxDataList = array();
foreach ($ajaxData as $key => $value) {
print_r($key);
foreach ($value as $data) {
$ajaxDataList[] = $data;
}
}
$dbDataList = array();
foreach ($dbData as $key => $value) {
print_r($key);
foreach ($value as $data) {
$dbDataList[] = $data;
}
}
var_dump(array_diff($ajaxDataList, $dbDataList));
来自代码的O / P: -
array (size=2)
4 => string 'Admin Phase 2 Task 3' (length=20)
7 => string 'Admin Phase 3 Task 3' (length=20)
我能找到两个数组之间的区别,但我无法找到带键的差异。 我只需要在这里添加密钥。但是我无法做到。
我需要这样的结构,
array (size=2)
'phase-6[]' => string 'Admin Phase 2 Task 3' (length=20)
'phase-7[]' => string 'Admin Phase 3 Task 3' (length=20)
答案 0 :(得分:1)
轻微解决方案,只需很少的更改
1.您需要为2 /
代码中的每个数组的所有值添加具有一些独特字符的键(我使用foreach()
)
2.获得差异后重复此数组并使用唯一字符进行爆炸(我使用/
)然后使用第一部分作为键,第二部分作为值并将它们分配给最终结果数组
如下所示: -
$ajaxDataList = array();
foreach ($ajaxData as $key => $value) {
foreach ($value as $data) {
$ajaxDataList[] = $key.'/'.$data; //add key with each value
}
}
$dbDataList = array();
foreach ($dbData as $key => $value) {
foreach ($value as $data) {
$dbDataList[] = $key.'/'.$data; // add key with each value
}
}
$semi_final_array = array_diff($ajaxDataList, $dbDataList);
$final_array = [];
foreach($semi_final_array as $arr){ // iterate over difference array
$exploded = explode('/',$arr); //explode data
$final_array[$exploded[0]] = $exploded[1]; // use exploded array as key value pair
}
print_r($final_array);
输出: - https://eval.in/984265
答案 1 :(得分:1)
写一个递归函数。
注意:你的子数组键必须相同,在第一个数组中,子数组键从1开始,但在第二个数组中,它从0开始。如果你可以使它相同,这个函数会工作
参考:http://php.net/manual/en/function.array-diff-assoc.php#73972
function array_diff_assoc_recursive($array1, $array2)
{
foreach($array1 as $key => $value)
{
if(is_array($value))
{
if(!isset($array2[$key]))
{
$difference[$key] = $value;
}
elseif(!is_array($array2[$key]))
{
$difference[$key] = $value;
}
else
{
$new_diff = array_diff_assoc_recursive($value, $array2[$key]);
if($new_diff != FALSE)
{
$difference[$key] = $new_diff;
}
}
}
elseif(!isset($array2[$key]) || $array2[$key] != $value)
{
$difference[$key] = $value;
}
}
return !isset($difference) ? 0 : $difference;
}
var_dump( array_diff_assoc_recursive( $ajaxData, $dbData) );
答案 2 :(得分:1)
你需要循环两个数组并相互比较 否则,如果一个阵列中存在一个子阵列但不存在另一个阵列,则不会记录差异。
//Loop once first array and compare against the other
Foreach($ajaxData as $key => $arr){
If(isset($dbData[$key])){
$ret[] = array_diff($arr, $dbData[$key]);
}else{
// If subarray only exists in one array add it to return array
$ret[] = $arr;
}
}
// Then the other way around
Foreach($dbData as $key => $arr){
If(isset($ajaxData[$key])){
If(!isset($ret[$key])) $ret[$key] = array_diff($arr, $ajaxData[$key]);
}else{
// If subarray only exists in one array add it to return array
$ret[] = $arr;
}
}
// echo found diff's.
Foreach($ret as $key => $val){
If(is_array($val)){
Foreach($val as $v){
Echo $key ." " . $v . "\n";
}
}
}