从另一个数组中删除具有匹配值的数组

时间:2016-03-10 13:21:28

标签: php arrays multidimensional-array

  $arr = Array
  (
      [0] => Array
          (
              [PropertyID] => 11
          )
      [1] => Array
          (
              [PropertyID] => 22
          )
      [2] => Array
          (
              [PropertyID] => 33
          )
      [3] => Array
          (
              [PropertyID] => 44
          )
  )

  $excludeIDs = Array(22, 11, 44);

期望的结果:

  Array ( [0] => Array ( [PropertyID] => 33 ) )

我想在$excludeIDS

中排除包含匹配PropertyID的数组

我怎么能这样做?

2 个答案:

答案 0 :(得分:-1)

在数组中使用unset()

foreach($arr as $key => $value){
     if(in_array($value['PropertyID'], $excludeIDs)){
         unset($arr[$key]);
     }
}
$arr = array_values($arr);
print_r($arr);

输出:

Array
(
    [0] => Array
        (
            [PropertyID] => 33
        )    
)

答案 1 :(得分:-1)

试试这个:

<?php 
$excludeIDs = array(22, 11, 44);
$counter = 0;
foreach ($arr as $key => $value) {
    if (!in_array($value['PropertyID'], $excludeIDs)) {
        $result_array[$counter] = $value;
        $counter++;
    }

}
echo '<pre>';
print_r($result_array);
echo '</pre>';
?>