3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php $testarr = array( 'advertiser' => array( 'id'=>345343, 'name'=>'Advertiser123', 'agency' => array( 'id'=>345343, 'name'=>'Agency456', 'organisation' => array( 'id'=>223334, 'name'=>'Organisation456' ) ) ) ); function getkeypath($arr, $lookup) { if (in_array($lookup, $arr)) { return array($lookup); } else { foreach ($arr as $key => $subarr) { if (is_array($subarr)) { $ret = getkeypath($subarr, $lookup); if ($ret) { $ret[] = $key; return $ret; } } } } return null; } echo '$testarr: ' . "\n"; print_r($testarr); echo "\n" . '7: ' . "\n" . print_r(getkeypath($testarr, 223334), true) . "\n";
based on pdUbZ
Output for 4.3.0 - 7.2.0
$testarr: Array ( [advertiser] => Array ( [id] => 345343 [name] => Advertiser123 [agency] => Array ( [id] => 345343 [name] => Agency456 [organisation] => Array ( [id] => 223334 [name] => Organisation456 ) ) ) ) 7: Array ( [0] => 223334 [1] => organisation [2] => agency [3] => advertiser )