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

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

报告相同问题?

悬赏问题

  • ¥20 基于决策树的数字信号处理,2ask 2psk 2fsk的代码,检查下报错的原因
  • ¥20 python作业求过程
  • ¥15 wincc已组态的变量过多
  • ¥60 如图:直线与椭圆X轴平行,求直线与椭圆任意一点的相切坐标计算公式
  • ¥50 如何用python使用opencv里的cv::cudacodec::VideoWriter函数对视频进行GPU硬编码
  • ¥100 c#solidworks 二次开发 工程图自动标边线法兰 等折弯尺寸怎么标
  • ¥15 halcon DrawRegion 提示错误
  • ¥15 FastAPI Uvicorn启动显示404
  • ¥15 centos7.9脚本,怎么排除特定的访问记录
  • ¥15 关于#Django#的问题:我的静态文件呢?