Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

Please help!

share|improve this question

3 Answers 3

up vote 2 down vote accepted

You have to return the value that you found, so change:

search2($subarray, $key);

To:

return search2($subarray, $key);
share|improve this answer
    
Thank you so much. I knew I was overlooking something important –  sisko Sep 25 '12 at 12:55

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

share|improve this answer

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.

share|improve this answer
    
This function give only one value. How to get multiple values. I have replaced return true to with return $key. –  Yatin Mistry Dec 5 '14 at 11:11

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.