如何检查数组是否在另一个数组中

时间:2014-01-28 08:01:59

标签: php arrays

我有某种具体问题。我有多维数组,看起来像:

array(2) {
  [0]=>
  array(6) {
    [0]=>
    array(9) {
      ["id"]=>
      int(9997)
      ["project_id"]=>
      int(327)
      ["projectType_id"]=>
      int(1)
      ["parameter"]=>
      string(11) "Web Server?"
      ["description"]=>
      NULL
      ["severity"]=>
      string(1) "1" 
    }
    [1]=>
    array(9) {
      ["id"]=>
      int(9998)
      ["project_id"]=>
      int(327)
      ["projectType_id"]=>
      int(1)
      ["parameter"]=>
      string(7) "Server?"
      ["description"]=>
      NULL
      ["severity"]=>
      string(1) "2"
     }
    [2]=>
    array(9) {
      ["id"]=>
      int(9999)
      ["project_id"]=>
      int(327)
      ["projectType_id"]=>
      int(1)
      ["parameter"]=>
      string(7) "Server?"
      ["description"]=>
      NULL
      ["severity"]=>
      string(1) "3"
    }
    [3]=>
    array(9) {
      ["id"]=>
      int(10000)
      ["project_id"]=>
      int(327)
      ["projectType_id"]=>
      int(1)
      ["parameter"]=>
      string(7) "Server?"
      ["description"]=>
      NULL
      ["severity"]=>
      string(1) "2"
    }
    [4]=>
    array(9) {
      ["id"]=>
      int(10001)
      ["project_id"]=>
      int(327)
      ["projectType_id"]=>
      int(1)
      ["parameter"]=>
      string(7) "Server?"
      ["description"]=>
      NULL
      ["severity"]=>
      string(1) "2"
    }
    [5]=>
    array(9) {
      ["id"]=>
      int(10002)
      ["project_id"]=>
      int(327)
      ["projectType_id"]=>
      int(1)
      ["parameter"]=>
      string(11) "Web Server?"
      ["description"]=>
      NULL
      ["severity"]=>
      string(1) "2"
    }
  }
  [1]=>
  array(5) {
    [0]=>
    array(9) {
      ["id"]=>
      int(8828)
      ["project_id"]=>
      int(298)
      ["projectType_id"]=>
      int(1)
      ["parameter"]=>
      string(11) "Web Server?"
      ["description"]=>
      NULL
      ["severity"]=>
      string(1) "1"
    }
    [1]=>
    array(9) {
      ["id"]=>
      int(8829)
      ["project_id"]=>
      int(298)
      ["projectType_id"]=>
      int(1)
      ["parameter"]=>
      string(7) "Server?"
      ["description"]=>
      NULL
      ["severity"]=>
      string(1) "3"
    }
    [2]=>
    array(9) {
      ["id"]=>
      int(8830)
      ["project_id"]=>
      int(298)
      ["projectType_id"]=>
      int(1)
      ["parameter"]=>
      string(7) "Server?"
      ["description"]=>
      NULL
      ["severity"]=>
      string(1) "2"
    }
    [3]=>
    array(9) {
      ["id"]=>
      int(8831)
      ["project_id"]=>
      int(298)
      ["projectType_id"]=>
      int(1)
      ["parameter"]=>
      string(7) "Server?"
      ["description"]=>
      NULL
      ["severity"]=>
      string(1) "2"
    }
    [4]=>
    array(9) {
      ["id"]=>
      int(8832)
      ["project_id"]=>
      int(298)
      ["projectType_id"]=>
      int(1)
      ["parameter"]=>
      string(11) "Web Server?"
      ["description"]=>
      NULL
      ["severity"]=>
      string(1) "2"
    }
  }
}

我想要做的是了解这些表之间的差异。我的意思是表1中有多少行,表2中没有这些行。表1中没有多少行,表2中有两行,两个表中有多少行等于。

我尝试在数组上创建循环检查每一行并进行比较。我的代码发布在下面。它让我无处可以..

foreach ($bigArray[0] as $new){
    foreach($bigArray[1] as $old){
        if ($new== $old)
            echo "it is the same";
        else
            echo "it is different";
    }
}

我得到的只是it is different所有时间......

有没有任何功能来计算这种东西? (第一列-ID对于每一行都是唯一的,因此不应该考虑它。

1 个答案:

答案 0 :(得分:2)

您必须将array_udiff()array_diff_assoc()结合使用。不知怎的,这样:

function compare($a, $b) {
    if (isset($a['id'])) unset($a['id']);
    if (isset($b['id'])) unset($b['id']);
    return count(array_diff_assoc($a, $b))? 0 : 1;
}

$result = array_udiff($bigArray[0], $bigArray[1], 'compare');
if (!count($result)) echo 'it is the same';