3

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

4

3 回答 3

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');
于 2012-11-14T23:19:52.500 回答
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;
}
于 2012-11-14T20:39:13.910 回答
0

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

默认情况下,它不会完全按照您的意愿行事;您必须编写一些代码来检查子键和诸如此类的东西。

于 2012-11-14T20:30:34.223 回答