我有一个数组,像这样:
$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] 删除。
我来说两句