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

时间:2012-11-14 20:27:33

标签: php arrays recursion

所以我写了一个函数,在一个数组中深入搜索两个级别,用于键和子键对。基本上它正在寻找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'

3 个答案:

答案 0 :(得分:3)

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');

答案 1 :(得分:2)

function searchArray($arr, $key, $subkey) {
    $it = new RecursiveIteratorIterator(new RecursiveArrayIterator($arr), RecursiveIteratorIterator::SELF_FIRST);
    $found = false;
    foreach($it as $k => $v) {
        if ($k == $key && !$found) $found = true;
        elseif ($k == $subkey && $found) return $v;
    }
    return false;
}

答案 2 :(得分:0)

尝试使用array_walk_recursive()http://us1.php.net/function.array-walk-recursive

默认情况下,它不会完全符合您的要求;你必须编写一些代码来检查子项和诸如此类的东西。