Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

recursion - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...