How to convert array keys to all uppercase or lowercase?

4

I'm getting data from a Webservice , through a response in JSON.

Convert this data from JSON to array , via json_decode . But one thing that's bothering me is the fact that the keys are coming with the name in CamelCase.

Something like this:

['Id' => 44, 'NumeroDoCliente' => 55, 'ProdutoCodigoNum' => 77]

My concern is that I've had trouble with "over-indulgence" (or clutter, as you please) in webservices where the data came from CamelCase. For example, the id would come as Id , and then "refactor" to ID , which caused a big problem for me, and stopped my system.

So I wanted to create a normalization of these indexes, turning them into upper case or lower case.

Is there any way to change the "case" of indexes in PHP? Is there any way to convert all keys to upper case or lower case?

    
asked by anonymous 23.08.2016 / 15:41

1 answer

5

There is, indeed, the function array_change_key_case , Wallace. See:

$arr = ['Id' => 44, 'NumeroDoCliente' => 55, 'ProdutoCodigoNum' => 77];

For upper case :

$upperKeys = array_change_key_case($arr, CASE_UPPER);
print_r($upperKeys); // Array ( [ID] => 44 [NUMERODOCLIENTE] => 55 [PRODUTOCODIGONUM] => 77 )

For lower case :

$lowerKeys = array_change_key_case($arr, CASE_LOWER);
print_r($lowerKeys); // Array ( [id] => 44 [numerodocliente] => 55 [produtocodigonum] => 77 )
    
23.08.2016 / 16:09