微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

PHP – 递归搜索数组的键和子键,成功返回键[‘子键]

所以我写了一个函数,在一个数组中深入搜索两个级别的密钥和子密钥对.基本上它正在寻找key [‘subkey’],如果找到它,则返回key [‘subkey’].

我正在寻找的是一种以真正递归的方式执行此操作的方法,并根据需要搜索多个级别,直到它到达数组的末尾并被强制返回false,或者找到值并返回它.

我在PHP上完全是新手,我用谷歌搜索了几个小时没有结果.任何指向正确的方向将不胜感激.

这是功能

function searchArray($array, $key, $subkey) {
    foreach ($array as $item){
        if (is_array($item) && isset($item[$key]) && isset($item[$key][$subkey])){
            return $item[$key][$subkey];
        } else {
            foreach ($item as $subitem){
                if (is_array($subitem) && isset($subitem[$key]) && isset($subitem[$key][$subkey])){
                    return $subitem[$key][$subkey]; 
                }
            }
        }
    }
}

样本数组:

"locales" => array(
    "America" => array(
        "locations" => array(
            "us" => array(
                "title" => "United States",
                "lang" => "en_US",
            ),
            "gl" => array(
            )
        )
    ),
    "Europe" => array(
        "locations" => array(
            "at" => array(
                "title" => "Österreich",
                "lang" => "de_DE",
            ),
            "fr" => array(
                "title" => "France",
                "lang" => "fr_FR",
            ),
            "de" => array(
                "title" => "Deutschland",
                "lang" => "de_DE",
            ),
            "it" => array(
                "title" => "Italy",
                "lang" => "it_IT",
            ),
            "uk" => array(
                "title" => "United Kingdom",
                "lang" => "en_GB",
            )
        )   
    ),
    "Africa" => array(
        "locations" => array(
            "za" => array(
                "title" => "Südafrika",
                "lang" => "en_ZA",
            )
        )
    ),
    "Asia & Pacific" => array(
        "locations" => array(
            "au" => array(
                "title" => "Australia",
                "lang" => "en_AU",
            ),
            "cn" => array(
                "title" => "中国",
                "lang" => "zh_CN",
            ),
            "hk" => array(
                "title" => "香港",
                "lang" => "zh_CN",
            ),
            "jp" => array(
                "title" => "日本",
                "lang" => "jp_JP",
            ),
            "kr" => array(
                "title" => "한국",
                "lang" => "ko_KR",
            )
        )
    )
)

调用searchArray($siteOptions [‘locales’],’us’,’lang’)应返回’en_US’.

解决方法:

function walk($array, $key, $option) {
    if( !is_array( $array)) {
        return false;
    }
    foreach ($array as $k => $v) {
        if($k == $key && is_array($v) && isset($v[$option])){
            return $v[$option];
        }
        $data = walk($v, $key, $option);
        if($data != false){
            return $data;        
        }
    }

    return false;
}

echo walk($array, 'jp', 'lang');

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐