php | 通过正则表达式在数组中查找

use*_*349 2 php regex

我正在寻找在字符串数组中找到一个字符串的方法:

$array = ['string1'=>'custom', 'other string'=>'another', 'another'=>'again', 'custom'=>'pid|100', 'once again'=>'pid|'];
Run Code Online (Sandbox Code Playgroud)

我需要找到这个元素:pid | 100 ;

字符串应该包含:pid |任何正整数

foreach($array as $value) { 
   // regular expression here
}
Run Code Online (Sandbox Code Playgroud)

我怎样才能找到这个价值?谢谢!

Mar*_*arc 10

你想使用preg_grep :)

print_r(preg_grep('/^pid\|[0-9]+$/',$array));
Run Code Online (Sandbox Code Playgroud)