php 如果项目值包含搜索的字符串字符,则从数组中删除项目

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9993168/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 08:02:51  来源:igfitidea点击:

Remove item from array if item value contains searched string character

phparraysstring

提问by Nicekiwi

I have an array built from the URL of a webpage.

我有一个从网页的 URL 构建的数组。

If an item in that array contains the ? symbol (The question mark symbol) then I want to remove that item from the array.

如果该数组中的一项包含 ? 符号(问号符号)然后我想从数组中删除该项目。

$array = 'news','artical','?mailchimp=1';

How could I do this? I've seen many examples where the searched string is the whole value, but not where its just a single character or just part of the value.

我怎么能这样做?我见过很多示例,其中搜索到的字符串是整个值,而不是它只是单个字符或值的一部分。

Thanks

谢谢

回答by Nameless

http://www.php.net/manual/en/function.array-filter.php

http://www.php.net/manual/en/function.array-filter.php

function myFilter($string) {
  return strpos($string, '?') === false;
}

$newArray = array_filter($array, 'myFilter');

回答by Mircea Soaica

foreach($array as $key => $one) {
    if(strpos($one, '?') !== false)
        unset($array[$key]);
}

回答by Lee Davis

Use a closure...

使用闭包...

$array = array_filter($array, function($value){
   if (strstr($value, '?') !== false)
   {
      return false;
   }
   return true;
});