I want to find the parent key of the second key in a multi dimensional array, like this:
Array
(
[REG-4] => Array
(
[9007] => MANADO
[9012] => MAMBAL
[9078] => SEMBUNG GEDE
[90A8] => BANYUWANGI
)
[REG-2] => Array
(
[9009] => SUBANG
[9013] => CITEUREUP
[9014] => BABAKAN PARI
[9017] => CIANJUR
[9018] => CIHERANG
[9052] => LEGOS
[9076] => SENTUL
[90A0] => CARINGIN
[9101] => MEKARSARI
[9103] => BEKASI
)
)
i have this code to achieve something like this => recursion(array,9007) = REG-4
function recursion($array,$needle,$parent=null){
foreach($array as $key => $value){
if(is_array($value)){
recursion($value,$needle,$parent=$key);
}
else if($key==$needle){
return $parent;
}
}
}
But my issue is that the return does not stop the recursion. After finding the correct parent, the function will still continue. Need some help if there is any wrong logic in my recursion function.
Best