3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php $testarr = array ( 'advertiser' => array ( 'id' => 135234, 'name' => 'Some Advertiser', 'agency' => array ( 'id' => 137734, 'name' => 'Some Agenecy', 'organisation' => array ( 'id' => 383734, 'name' => 'Some Organisation' ), ) ) ); 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 '<br>' . 'Organisation: ' . '<br>' . print_r(getkeypath($testarr, '383734'), true) . '<br>';
based on b1mkO
Output for 4.3.0 - 7.2.0
$testarr: Array ( [advertiser] => Array ( [id] => 135234 [name] => Some Advertiser [agency] => Array ( [id] => 137734 [name] => Some Agenecy [organisation] => Array ( [id] => 383734 [name] => Some Organisation ) ) ) ) <br>Organisation: <br>Array ( [0] => 383734 [1] => organisation [2] => agency [3] => advertiser ) <br>