PHP递归数组搜索 [英] PHP recursive array searching

查看:58
本文介绍了PHP递归数组搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下函数递归搜索数组:

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] => email1@test.org
            [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
        )
)

我的函数将一直调用自身,直到找到要搜索的索引为止.如果我要搜索 email_address ,如果该数组键存在,则if语句的第一部分应返回该索引的值,否则它将在代码的第二部分进入递归模式.

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]}");

但是,我希望return语句可以实现预期的功能,但在函数调用时却没有输出.

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.

推荐答案

您必须返回找到的值,因此请更改:

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

search2($subarray, $key);

收件人:

return search2($subarray, $key);

这篇关于PHP递归数组搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆