-3

I'm using this recursive function to get the grand parent of an option. As far as the needle exists in the array keys, it mean that needle has a parent. When not found, then the needle do not have any parent, so it must return the needle itself.

get_parent_need(165,$hay);
function get_parent_need($needle,$hay){
    if(in_array($needle,array_keys($hay))){
        get_parent_need($hay[$needle],array_keys($hay));
    }else{
        return $needle;
    }
}

array image

Majid Ali
  • 109
  • 6

1 Answers1

0

No need to use recursion you can search array using

function get_parent_need($needle,$hay) {
    if(! in_array( $needle, array_keys( $hay ) ) ){
        return $needle;
    }
    return $hay;
}
Jerson
  • 1,700
  • 2
  • 10
  • 14
  • No, its not what I need. I need to keep this function running until it find no needle in the array, then I would need that needle to be returned. – Majid Ali Nov 19 '20 at 15:36