使用正则表达式在数组中查找某个值的最短代码?

use*_*596 0 php arrays

假设我有一个包含这些元素的数组:

[
    'whatever',
    'something else',
    'foobar'
]
Run Code Online (Sandbox Code Playgroud)

我知道我可以迭代数组并使用正则表达式检查foobar.问题是,有没有更短的方法呢?

谢谢?

thp*_*hpl 9

<?php

$items = [
    'whatever',
    'something else',
    'foobar'
];

$matches  = preg_grep ('/foobar/', $items);
var_dump($matches);

?>
Run Code Online (Sandbox Code Playgroud)

您不需要遍历数组,您可以使用preg_grep并传递您的数组.

快乐的编码