Blog de programación, errores, soluciones

Chose Language:
Author: Admin/Publisher |finished | checked

PHP array_change_key_case function

The PHP function array_change_key_case changes the keys to uppercase or lowercase depending on whether we specify CASE_UPPER or CASE_LOWER.

Description / Descripción
array_change_key_case(array $array, int $case = CASE_LOWER): array

In case it is not specified whether the keys should be in lowercase or uppercase, it will take its default value, which is CASE_LOWER (lowercase).

This function will return a new array with all keys in lowercase or uppercase as specified.

Sintaxis / Sintax
$narray = array_change_key_case($array);
$narray = array_change_key_case($array, CASE_UPPER);

Parameters

$array – The array to which we will change the keys to uppercase or lowercase.

$case – can have 2 values: CASE_UPPER (Uppercase) or CASE_LOWER (Lowercase), in case it is not specified, it will use the constant CASE_LOWER.

Returns

array – This will be a new array with the keys in uppercase or lowercase.

PHP array_change_key_case example

In the following example, we will see its default behavior:

default value
$persona = array(
    "Nombre" => "Juan",
    "Edad" => 25,
    "Ciudad" => "EjemploCity"
);

$narray = array_change_key_case($persona);
print_r($narray);
Array
(
    [nombre] => Juan
    [edad] => 25
    [ciudad] => EjemploCity
)

In the second example, let’s consider the case where we use CASE_UPPER:

use of CASE_UPPER
$persona = array(
    "Nombre" => "Juan",
    "Edad" => 25,
    "Ciudad" => "EjemploCity"
);

$narray = array_change_key_case($persona,CASE_UPPER);
print_r($narray);
Array
(
    [NOMBRE] => Juan
    [EDAD] => 25
    [CIUDAD] => EjemploCity
)
Category: en-php
Something wrong? If you found an error or mistake in the content you can contact me on Twitter | @luisg2249_luis.
Last 4 post in same category