3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php $a = array('abc7', 'cbd4', 'def9'); $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);
Output for 4.3.0 - 5.6.28, hhvm-3.10.0 - 3.12.0, 7.0.0 - 7.1.0
Array ( [r1] => abc7 [b2] => cbd4 [f5] => def9 )