[英]Remove all array elements that do not match specific condition
我一直在研究array_map
但不确定这是否是最好的方法。
我目前正在从我的scandir
返回以下数组
Array ( [0] => . [1] => .. [2] => .DS_Store [3] => post-my-first-test-post.html [4] => post-my-second-test-post.html )
我想删除数组中与"post-*
不匹配"post-*
所有项目,因此它仅返回帖子数组。
我已经研究过使用glob
但不确定如何使用现有的方法来实现它。
像glob("post-*");
类的东西glob("post-*");
?
您应该查看array_filter()
$result = array_filter(
$originalArray,
function($value) {
return (strpos($value, 'post-') === 0);
}
);
尽管使用glob()而不是scandir()将允许您在实际检索目录列表时进行过滤
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.