删除所有与特定条件不匹配的数组元素

Fra*_*sca 1 php arrays

我一直在研究,array_map但不确定这是否是最好的方法.

我目前正从我的网站返回以下数组 scandir

Array ( [0] => . [1] => .. [2] => .DS_Store [3] => post-my-first-test-post.html [4] => post-my-second-test-post.html )
Run Code Online (Sandbox Code Playgroud)

我想删除数组中不匹配的所有项目,"post-*因此它只返回一组帖子.

我已经研究过使用glob但不确定如何使用我拥有的东西来实现它.

有点像glob("post-*");

Mar*_*ker 7

你应该看array_filter()代替

$result = array_filter(
    $originalArray,
    function($value) {
        return (strpos($value, 'post-') === 0);
    }
);
Run Code Online (Sandbox Code Playgroud)

虽然使用glob()而不是scandir()会允许您在实际检索目录列表时执行过滤