How to convert array values to lowercase?

04/09/2020 11:00:02

How can I convert all values in an array to lowercase in PHP?

Something like array_change_key_case?

Verified Answer (337 Votes)

06/13/2012 10:03:49

use array_map():

$yourArray = array_map('strtolower', $yourArray);

In case you need to lowercase nested array (by Yahya Uddin):

$yourArray = array_map('nestedLowercase', $yourArray);

function nestedLowercase($value) {
    if (is_array($value)) {
        return array_map('nestedLowercase', $value);
    return strtolower($value);

Answer #2 (25 Votes)

12/14/2013 21:07:51

Just for completeness: you may also use array_walk:

array_walk($yourArray, function(&$value)
  $value = strtolower($value);

From PHP docs:

If callback needs to be working with the actual values of the array, specify the first parameter of callback as a reference. Then, any changes made to those elements will be made in the original array itself.

Or directly via foreach loop using references:

foreach($yourArray as &$value)
  $value = strtolower($value);

Note that these two methods change the array "in place", whereas array_map creates and returns a copy of the array, which may not be desirable in case of very large arrays.


Answer #3 (8 Votes)

06/13/2012 10:04:33

You could use array_map(), set the first parameter to 'strtolower' (including the quotes) and the second parameter to $lower_case_array.

Hack Hex uses Stack Exchance API by the Stack Exchange Inc. to scrape questions/answers under Creative Commons license.