3v4l.org

run code in 300+ PHP versions simultaneously
<?php $ar = []; $ar['ar0']['y'] = 'Yes'; $ar['ar0']['n'] = 'No'; $ar['ar1']['g'] = 'good'; $ar['ar1']['b'] = 'bad'; $ar['ar2']['m'] = 'mom'; $ar['ar2']['d'] = 'dad'; $ar['ar4']['y'] = 'Yes'; $ar['ar4']['n'] = 'No'; print_r($ar); $res_ar = []; // resultant array function getArrayByKey($needle, $haystack) { foreach ($haystack as $arr) { if (in_array($needle, $arr)) { return $arr; } } return []; } $res_ar = getArrayByKey('Yes', $ar); print_r($res_ar);
Output for 7.1.25 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.26, 7.4.0 - 7.4.13, 8.0.0 - 8.0.3
Array ( [ar0] => Array ( [y] => Yes [n] => No ) [ar1] => Array ( [g] => good [b] => bad ) [ar2] => Array ( [m] => mom [d] => dad ) [ar4] => Array ( [y] => Yes [n] => No ) ) Array ( [y] => Yes [n] => No )