3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php $data = array( "abc"=>array( "label" => "abc", "value" => "def", "type" => "ghi", "desc" => "jkl", ), "def"=>array( "label" => "mno", "value" => "qrs", "type" => "tuv", "desc" => "wxyz", ), ); $matches = array(); $pattern = "/a/i"; //contains an 'a' //loop through the data foreach($data as $key=>$value){ //loop through each key under data sub array foreach($value as $key2=>$value2){ //check for match. if(preg_match($pattern, $value2)){ //add to matches array. $matches[$key]=$value; //match found, so break from foreach break; } } } echo '<pre>'.print_r($matches, true).'</pre>';
Output for 4.3.0 - 5.6.28, hhvm-3.10.0 - 3.12.0, 7.0.0 - 7.1.0
<pre>Array ( [abc] => Array ( [label] => abc [value] => def [type] => ghi [desc] => jkl ) ) </pre>