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

php |通过正则表达式在数组中查找,php,regex,Php,Regex,我正在寻找在字符串数组中查找一个字符串的方法: $array = ['string1'=>'custom', 'other string'=>'another', 'another'=>'again', 'custom'=>'pid|100', 'once again'=>'pid|']; 我需要找到这个元素:pid | 100 字符串应包含:pid |任何正整数 foreach($array as $value) { // regular express

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

$array = ['string1'=>'custom', 'other string'=>'another', 'another'=>'again', 'custom'=>'pid|100', 'once again'=>'pid|'];
我需要找到这个元素:pid | 100

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

foreach($array as $value) { 
   // regular expression here
}
如何找到该值? 谢谢

您要用于此:)

in_数组(preg_match(“/^pid\[0-9]+$/”,$your_数组),$your_数组)

in_数组(preg_grep(“/^pid\[0-9]+$/”,$your_数组),$your_数组)


如果您需要检查此特定元素,即
pid | 100
,则只需在|数组('pid | 100',$your |数组)
中使用
,无需使用正则表达式进行检查


希望这将帮助您解决问题

请查看我的最新问题。我有另外一个数组,它应该对关联数组起同样的作用。如果您只需要返回preg_grep时可以使用的值来索引结果。谢谢。请看一下我的最新问题。我的输入数据有另一个结构。如果需要检查此特定元素,即
pid | 100
,则只需在|数组中使用
('pid | 100',$your | array)
,无需使用正则表达式进行检查
print_r(preg_grep('/^pid\|[0-9]+$/',$array));