PHP recursive array searching

I am using the following function to search through an array recursively:

function search2($array, $key){
    if( array_key_exists($key, $array) ){
        print("<br> ----------------- FOUND <u>{$key}</u> with value: {$array[$key]}");

        return array( $key => $array[$key] );

    }else if( !array_key_exists($key, $array) ){
        foreach ($array as $index   =>  $subarray){
                if( is_array($subarray) ){
                    print("<br> ************* <u>{$index}</u> is an ARRAY");
                    print("<br> ************* RE-SEACHING <u>{$index}</u> FOR : <u>{$key}</u>");
                    search2($subarray, $key);
                }
        }
    }
}

So, with the following array structure:

Array
(
    [personal] => Array
        (
            [title] => 
            [forename] => 
            [surname] => 
            [post_code] => 
            [date_of_birth] => Array
                (
                    [month] => 
                    [day] => 
                    [year] => 
                )

            [email_address] => [email protected]
            [confirm_email_address] => 
            [mobile_telephone] => 
            [home_telephone] => 
            [work_telephone] => 
            [are_you_entering_fundraising_in_a_team] => Array
                (
                    [yes] => 0
                )

            [how_many_places_would_you_like] => 
            [team_name] => 
            [names_of_team_members] => 
            [how_did_you_hear_about_this_event] => 
            [please_tell_us_] => 
            [would_you_be_happy_for_publicity] => 
            [is_this_the_first_time_you_have_taken_part_in_or_attended_this_event] => Array
                (
                    [yes] => 0
                )

            [do_you_have_a_special_reason_for_taking_part_in_or_attending_this_event] => 
            [what_are_your_plans_for_raising_the_minimum_sponsorship_amount___please_be_as_detailed_as_possible] => 
            [number_of_tickets_required] => 1
        )
)

My function will keep calling itself until it finds an index I am searching for. If I were searching for email_address, the first part of the if statement should return the value of that index if that array key exists otherwise it goes into recursive mode in the second part of the code.

The trouble is the code seems to work because I get my "found" print out statement as in the following:

print("<br> ----------------- FOUND <u>{$key}</u> with value: {$array[$key]}");

However, I expect the return statement to do what it's supposed to but I get no output at the point of my function call.


Also you can use built-in function array_walk_recursive with callback supplied.


Use a recursive in array function:

function in_array_r ( $needle, $haystack, $strict = true )
    {
        foreach ( $haystack as $value )
        {
            if (( $strict ? $value === $needle : $value == $needle ) || ( is_array ( $value ) && in_array_r ( $needle, $value, $strict )))
            {
                return true;
            }
        }

        return false;
    }

This function will keep on looping until it finds the key you're looking for.