How can I convert all values in an array to lowercase in PHP?
Something like array_change_key_case
?
If you wish to lowercase all values in an nested array, use the following code:
function nestedLowercase($value) {
if (is_array($value)) {
return array_map('nestedLowercase', $value);
}
return strtolower($value);
}
So:
[ 'A', 'B', ['C-1', 'C-2'], 'D']
would return:
[ 'a', 'b', ['c-1', 'c-2'], 'd']
You can also use a combination of array_flip()
and array_change_key_case()
. See this post
You don't say if your array is multi-dimensional. If it is, array_map will not work alone. You need a callback method. For multi-dimensional arrays, try array_change_key_case.
// You can pass array_change_key_case a multi-dimensional array,
// or call a method that returns one
$my_array = array_change_key_case(aMethodThatReturnsMultiDimArray(), CASE_UPPER);
`$Color = array('A' => 'Blue', 'B' => 'Green', 'c' => 'Red');
$strtolower = array_map('strtolower', $Color);
$strtoupper = array_map('strtoupper', $Color);
print_r($strtolower); print_r($strtoupper);`