Organizational Research By

Surprising Reserch Topic

Question:array_change_key_case: how to use array_change_key_case using php ?


 

$input_array = array("FirSt" => 1, "SecOnd" => 4);
print_r(array_change_key_case($input_array, CASE_UPPER));
?>
The above example will output:
Array
(
    [FIRST] => 1
    [SECOND] => 4
)

asked Sep 13, 2013 in PHP by ashish singh
edited Sep 12, 2013
0 votes
36 views



Related Hot Questions

3 Answers

0 votes

 

function array_change_value_case($input, $case = CASE_LOWER)
{
    $aRet = array();
    
    if (!is_array($input))
    {
        return $aRet;
    }
    
    foreach ($input as $key => $value)
    {
        if (is_array($value))
        {
            $aRet[$key] = array_change_value_case($value, $case);
            continue;
        }
        
        $aRet[$key] = ($case == CASE_UPPER ? strtoupper($value) : strtolower($value));
    }
    
    return $aRet;
}
?>
answered Sep 13, 2013 by ashish singh
edited Sep 12, 2013
0 votes

 

Script to change case of sub-arrays keys:
 
 
    function arrKey2Lower(&$arrVals) {
        foreach( $arrVals as $key => $item ) {
            $key2 = strtolower($key);
            if ( $key2 != $key) {
                unset($arrVals[$key]);
                $arrVals[$key2]=$item;
                $key=$key2;
            }
            
            if ( is_array($item) ) { arrKey2Lower($arrValores[$key]); }
        }
    }
    
    $arr = array('ID' => 1, 'NAME'=> 'Luis', 'Contact' => array('PHONE' => '3010-7148', 'E-MAIL' => 'luis@net.com') );
    arrKey2Lower($arr);
    var_dump($arr);
    
    
?>
 
outputs:
 
array(3) {
  ["id"]=>
  int(1)
  ["name"]=>
  string(4) "Luis"
  ["contact"]=>
  array(2) {
    ["phone"]=>
    string(9) "3010-7148"
    ["e-mail"]=>
    string(12) "luis@net.com"
  }
}
answered Sep 13, 2013 by ashish singh
edited Sep 12, 2013
0 votes

 

function array_change_key_case_unicode($arr, $c = CASE_LOWER) {
    $c = ($c == CASE_LOWER) ? MB_CASE_LOWER : MB_CASE_UPPER;
    foreach ($arr as $k => $v) {
        $ret[mb_convert_case($k, $c, "UTF-8")] = $v;
    }
    return $ret;
}
 
$arr = array("FirSt" => 1, "yağ" => "Oil", "şekER" => "sugar");
print_r(array_change_key_case($arr, CASE_UPPER));
print_r(array_change_key_case_unicode($arr, CASE_UPPER));
?>
 
Array
(
    [FIRST] => 1
    [YAğ] => Oil
    [şEKER] => sugar
)
Array
(
    [FIRST] => 1
    [YAĞ] => Oil
    [ŞEKER] => sugar
)
answered Sep 13, 2013 by ashish singh
edited Sep 12, 2013

...