doupi1532 2012-01-18 20:38
浏览 67
已采纳

PHP:检查两个multidim数组之间的差异

Here's the two arrays dump:

array(2) { 
    [0]=> array(7) { 
        ["id"]=> string(1) "1" 
        ["shoppinglist_id"]=> string(1) "1" 
        ["product_id"]=> string(1) "1" 
        ["amount"]=> string(1) "5" 
        ["date_added"]=> string(10) "1326912709"
        ["name"]=> string(7) "Tunfisk" 
        ["supplier_id"]=> string(1) "2" 
    } 
    [1]=> array(7) { 
        ["id"]=> string(1) "2" 
        ["shoppinglist_id"]=> string(1) "1" 
        ["product_id"]=> string(1) "2" 
        ["amount"]=> string(1) "5" 
        ["date_added"]=> string(10) "1326912713" 
        ["name"]=> string(3) "Lax" 
        ["supplier_id"]=> string(1) "6" 
    } 
}

array(2) { 
    [0]=> array(7) { 
        ["id"]=> string(1) "5" 
        ["shoppinglist_id"]=> string(1) "3" 
        ["product_id"]=> string(1) "1" 
        ["amount"]=> string(1) "5" 
        ["date_added"]=> string(10) "1326912810" 
        ["name"]=> string(7) "Tunfisk" 
        ["supplier_id"]=> string(1) "2" 
    } 
    [1]=> array(7) { 
        ["id"]=> string(1) "6" 
        ["shoppinglist_id"]=> string(1) "3" 
        ["product_id"]=> string(1) "2" 
        ["amount"]=> string(1) "5" 
        ["date_added"]=> string(10) "1326912810" 
        ["name"]=> string(3) "Lax" 
        ["supplier_id"]=> string(1) "6" 
    } 
}

I tried to do array_diff() but this does not support multi-dimensional arrays.

So then i tried this function:

public function multidimensional_array_diff($a1,$a2)
{
  $r = array();

 foreach ($a2 as $key => $second)
 {
      foreach ($a1 as $key => $first)
      {

            if (isset($a2[$key]))
            {
                foreach ($first as $first_arraykey => $first_value)
                {

                    foreach ($second as $second_value)
                    {
                        if ($first_value == $second_value)
                        {
                            $true = true;
                            break;   
                        }
                    }
                    if (!isset($true))
                    {
                        if($first_arraykey != "date_added" && $first_arraykey != "shoppinglist_id")
                        {
                            $r[$key][$first_arraykey] = $first_value;
                        }

                    }
                    unset($true);
                }
            }
            else
            {
                $r[$key] = $first;
            }
      }
 }
  return $r;
}

This does not work either, returns me differences, that arent different.

Note as you can see it does not add to the difference array if the array key are date_added and shoppinglist_id (because, it's OK in my system that these are different).

The above should output an empty difference array, since there are no difference between those array (if we dont look at date_added and shoppinglist_id).

How can i make this work properly?

  • 写回答

1条回答 默认 最新

  • douchangmian0305 2012-01-18 20:49
    关注

    In the comments to array_diff, some recursive examples are given. This one seems to do, what you are searching for:

    <?php 
    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; 
    } 
    ?>
    

    Source: http://www.php.net/manual/en/function.array-diff.php#91756

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧