我有一个包含文件路径的and数组,但我需要过滤掉某些文件夹。
这只是一个例子,但我想删除在本例中位于'thumbs','database‘和'test’文件夹中的文件。
我找不到用于此操作的数组筛选器。
[
[0] => uploads/projects/pathtofile.jpg,
[1] => uploads/projects/thumbs/pathtofile.jpg,
[3] => uploads/database/projects/pathtofile.jpg,
[4] => uploads/projects/thumbs/database/pathtofile.jpg,
[5] => uploads/thumbs/projects/test/pathtofile.jpg
]发布于 2018-01-22 19:47:02
您可以使用array
foreach($array as $key=> $ar){
if(count(array_intersect(explode('/',$ar),['thumbs', 'database','test']))>0){
unset($array[$key]);
}
}发布于 2018-01-22 19:48:32
使用array_filter callable怎么样?
<?php
$a = array(
0 => 'uploads/projects/pathtofile.jpg',
1 => 'uploads/projects/thumbs/pathtofile.jpg',
3 => 'uploads/database/projects/pathtofile.jpg',
4 => 'uploads/projects/thumbs/database/pathtofile.jpg',
5 => 'uploads/thumbs/projects/test/pathtofile.jpg',
6 => 'uploads/thumb/projects/pathtofile.jpg'
);
$b = array_values(array_filter($a, function ($item) {
return !preg_match('/(\/thumbs\/|\/database\/|\/test\/)/', $item);
}));
echo '<pre>' . print_r($b, 1) . '</pre>';输出:
Array
(
[0] => uploads/projects/pathtofile.jpg
[1] => uploads/thumb/projects/pathtofile.jpg
)发布于 2018-01-22 19:52:10
我会选择array_filter()和preg_match()
$filtered = array_filter($a, function($i) {
return preg_match('/(thumbs|database|test)/', $i) !== 1;
});https://stackoverflow.com/questions/48380811
复制相似问题