繁体   English   中英

删除所有不符合特定条件的数组元素

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM