假设我有一个包含这些元素的数组:
[ 'whatever', 'something else', 'foobar' ]
我知道我可以迭代数组并使用正则表达式检查foobar.问题是,有没有更短的方法呢?
谢谢?
<?php $items = [ 'whatever', 'something else', 'foobar' ]; $matches = preg_grep ('/foobar/', $items); var_dump($matches); ?>
您不需要遍历数组,您可以使用preg_grep并传递您的数组.
快乐的编码