使用另一个数组过滤多维php数组

时间:2018-01-26 04:50:36

标签: php arrays multidimensional-array

我有一个数组$result

[0] => Array (
  [0] => Array (
    [itemid] => 1
    [name] => A
  )
  [1] => Array (
    [itemid] => 2
    [name] => B
  )
)
[1] => Array (
  [0] => Array (
    [itemid] => 3
    [name] => C
  )
  [1] => Array (
    [itemid] => 2
    [name] => B
  )
)

和数组$items如下:

[0] => Array (
  [itemid] => 2
  [name] => B
)
[1] => Array (
  [itemid] => 4
  [name] => D
) 

如何删除$result数组中出现的$items数组中的所有项?在这种情况下,$result将成为:

[0] => Array (
  [0] => Array (
    [itemid] => 1
    [name] => A
  )
)
[1] => Array (
  [0] => Array (
    [itemid] => 3
    [name] => C
  )
)

由于问题主要是代码,因此这里有一些额外的字符可以让StackOverflow接受这个问题。

1 个答案:

答案 0 :(得分:1)

我认为这就是你想要的。 (尚未测试)

<?php
foreach ($result as $key => $array) {
    $result[$key] = array_diff($array, $items);
}

print_r($result);