删除数组中的术语

亨里克·佩特森

我有一个数组,像这样:

$array = array(
  'hello',
  'world',
  'lorem',
  'ipsum',
  'what',
  'is',
  'reality'
);

我有一个字符串变量:

$terms = 'Hello World is reality!';

因此,如果$array包含中的任何术语$terms,那么我想从数组中删除这些术语因此,在这种情况下,$terms最终将是:

$array = array(
  'lorem',
  'ipsum',
  'what'
);

什么是实现这一目标的最佳方法?

威廉·贾诺蒂
$terms = 'Hello World is reality!';
$result = array_filter(array_map(function ($word) use ($terms) {
    if (!stristr($terms, $word)) {
       return $word;
    }
},$array));

输出:

array(3) {
  [2] =>
   string(5) "lorem"
  [3] =>
   string(5) "ipsum"
  [4] =>
   string(4) "what"
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章