3v4l.org

run code in 300+ PHP versions simultaneously
<?php $a = array( 'def9', 'abc7', 'cbd4' ); $b = array('r1' => '/abc\d/', 'b2' => '/cbd\d/', 'f5' => '/def\d/'); $result = array(); // Store the result $iloop = 0; // Use to match corresponding element key in $a; $matches = array(); foreach($b AS $key => $preg) { if (preg_match($preg, $a[$iloop++], $matches)) { $result[$key] = $matches[0]; } } print_r($result);

preferences:
35.26 ms | 402 KiB | 5 Q