Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如果某个元素的部分名称存在,我可以从php中的数组中删除该元素吗?_Php_Arrays - Fatal编程技术网

如果某个元素的部分名称存在,我可以从php中的数组中删除该元素吗?

如果某个元素的部分名称存在,我可以从php中的数组中删除该元素吗?,php,arrays,Php,Arrays,我目前有一个文件名数组,这些文件名取自具有以下内容的目录: fileOne.txt fileTwo.txt fileOne_Results.txt fileThree.txt 我的代码: $index = array_search('_Results',$extension); if($index !== FALSE) { unset($extension[$index]); print_r($extension); } 我目前正在目录中循环并在表中显示文件,但我不

我目前有一个文件名数组,这些文件名取自具有以下内容的目录:

fileOne.txt 
fileTwo.txt
fileOne_Results.txt
fileThree.txt
我的代码:

$index = array_search('_Results',$extension);
    if($index !== FALSE) {
    unset($extension[$index]);
    print_r($extension);
}
我目前正在目录中循环并在表中显示文件,但我不想显示名称中包含“\u Results”的文件

这可能吗

理想情况下,我希望从数组中删除它们,我尝试了unset方法,但它只适合完整的文件名。 提前谢谢

你可以用它。回调可以如下所示:

function showFile($file) {
  return strpos($file, "_Results") < 0;
}

$new_array = array_filter($old_array, 'showFile')
函数showFile($file){
返回strpos($file,“\u结果”)<0;
}
$new\u array=array\u filter($old\u array,'showFile'))

或者,您也可以使用以下正则表达式
(\u结果)
,如

$array = array('fileOne.txt','fileTwo.txt','fileOne_Results.txt','fileThree.txt');
$result = preg_grep("/(_Results)/",$array,PREG_GREP_INVERT);
print_r($result);
输出:

Array
(
    [0] => fileOne.txt
    [1] => fileTwo.txt
    [3] => fileThree.txt
)

使用
strpos
在循环中添加检查。用于检查\u结果是否是值的一部分。效果非常好!谢谢你,我必须先赢得更多的声誉才能做到这一点!不过我一拿到它就去做,再次谢谢@使用者