删除不符合特定条件的所有数组元素 [英] Remove all array elements that do not match specific condition
本文介绍了删除不符合特定条件的所有数组元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我一直在寻找到 array_map
,但不知道这是否是做的最好的方式。
I've been looking into array_map
but not sure if this is the best way to do it.
我目前得到以下阵列从回我 SCANDIR
I am currently getting the following array returned from my scandir
Array ( [0] => . [1] => .. [2] => .DS_Store [3] => post-my-first-test-post.html [4] => post-my-second-test-post.html )
我想删除阵列中的所有项目不符合后 - *。
所以它只返回岗位的数组
I want to remove all items in the array that do not match "post-*
so it returns only an array of posts.
我看着使用水珠
,但不知道如何与我有什么实现它。
I have looked into using glob
but not sure how to implement it with what I have.
类似水珠(后 - *);
推荐答案
您应该看 array_filter()代替
$result = array_filter(
$originalArray,
function($value) {
return (strpos($value, 'post-') === 0);
}
);
虽然使用水珠()而不是SCANDIR()将允许你做过滤器,而实际上检索目录列表
Though using glob() rather than scandir() would have allowed you to do the filter while actually retrieving the directory list
这篇关于删除不符合特定条件的所有数组元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文