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

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

WBOY
Release: 2016-06-13 09:39:21
Original
1301 people have browsed it

//递归返回字符串键名全为小写或大写的数组
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);
        }
    }
}

Related labels:
source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template