Home >php教程 >php手册 >recurse_array_change_key_case()递规返回字符串键名全为小写或大写的数组

recurse_array_change_key_case()递规返回字符串键名全为小写或大写的数组

WBOY
WBOYOriginal
2016-06-13 09:39:211312browse

//递归返回字符串键名全为小写或大写的数组
function recurse_array_change_key_case(&$input, $case = CASE_LOWER){
    if(!is_array($input))
        return;

    foreach($input as $key => $val)
    {
        //1
        if($case == CASE_UPPER)
        {
            $newkey = strtoupper($key);
        }
        //0
        elseif($case == CASE_LOWER)
        {
            $newkey = strtolower($key);
        }
        
        if($newkey != $key)
        {
            unset($input[$key]);
            $input[$newkey] = $val;
        }
        if(is_array($val))
        {
            //###注:此处的参数须为$input[$newkey],而不是$val,如果是$val,需要在foreach中 $key=>&$val
            recurse_array_change_key_case($input[$newkey], $case);
        }
    }
}

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn