164

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

Something like array_change_key_case?

LihO
  • 41,190
  • 11
  • 99
  • 167
user1163513
  • 4,087
  • 7
  • 24
  • 25

11 Answers11

434

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);
}
ariefbayu
  • 21,849
  • 12
  • 71
  • 92
32

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.

Alex Shesterov
  • 26,085
  • 12
  • 82
  • 103
  • 1
    For multi-dimensional arrays, use `array_walk_recursive()`. Also `mb_strtolower()` because the World is multilingual. – kodeart Jun 21 '19 at 09:57
8

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

verisimilitude
  • 5,077
  • 3
  • 30
  • 35
7
$Color = array('A' => 'Blue', 'B' => 'Green', 'c' => 'Red');

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

$strtoupper = array_map('strtoupper', $Color);

print_r($strtolower);
print_r($strtoupper);`
Pragnesh Chauhan
  • 8,363
  • 9
  • 42
  • 53
5

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']   
Yahya Uddin
  • 26,997
  • 35
  • 140
  • 231
3

array_change_value_case

by continue

    function array_change_value_case($array, $case = CASE_LOWER){
        if ( ! is_array($array)) return false;
        foreach ($array as $key => &$value){
            if (is_array($value))
            call_user_func_array(__function__, array (&$value, $case ) ) ;
            else
            $array[$key] = ($case == CASE_UPPER )
            ? strtoupper($array[$key])
            : strtolower($array[$key]);
        }
        return $array;
    }


    $arrays = array ( 1 => 'ONE', 2=> 'TWO', 3 => 'THREE',
                     'FOUR' => array ('a' => 'Ahmed', 'b' => 'basem',
                     'c' => 'Continue'),
                      5=> 'FIVE',
                      array('AbCdeF'));


    $change_case = array_change_value_case($arrays, CASE_UPPER);
    echo "<pre>";
    print_r($change_case);
Array
(
 [1] => one
 [2] => two
 [3] => three
 [FOUR] => Array
  (
   [a] => ahmed
   [b] => basem
   [c] => continue
  )

 [5] => five
 [6] => Array
  (
   [0] => abcdef
  )

)
2

array_map() is the correct method. But, if you want to convert specific array values or all array values to lowercase one by one, you can use strtolower().

for($i=0; $i < count($array1); $i++) {
    $array1[$i] = strtolower($array1[$i]);
}
Pang
  • 9,564
  • 146
  • 81
  • 122
Jasmeen
  • 876
  • 9
  • 16
1

AIO Solution / Recursive / Unicode|UTF-8|Multibyte supported!

/**
 * Change array values case recursively (supports utf8/multibyte)
 * @param array $array The array
 * @param int $case Case to transform (\CASE_LOWER | \CASE_UPPER)
 * @return array Final array
 */
function changeValuesCase ( array $array, $case = \CASE_LOWER ) : array {
    if ( !\is_array ($array) ) {
        return [];
    }

    /** @var integer $theCase */
    $theCase = ($case === \CASE_LOWER)
        ? \MB_CASE_LOWER
        : \MB_CASE_UPPER;

    foreach ( $array as $key => $value ) {
        $array[$key] = \is_array ($value)
            ? changeValuesCase ($value, $case)
            : \mb_convert_case($array[$key], $theCase, 'UTF-8');
    }

    return $array;
}

Example:

$food = [
    'meat' => ['chicken', 'fish'],
    'vegetables' => [
        'leafy' => ['collard greens', 'kale', 'chard', 'spinach', 'lettuce'],
        'root'  => ['radish', 'turnip', 'potato', 'beet'],
        'other' => ['brocolli', 'green beans', 'corn', 'tomatoes'],
    ],
    'grains' => ['wheat', 'rice', 'oats'],
];

$newArray = changeValuesCase ($food, \CASE_UPPER);

Output

    [
    'meat' => [
        0 => 'CHICKEN'
        1 => 'FISH'
    ]
    'vegetables' => [
        'leafy' => [
            0 => 'COLLARD GREENS'
            1 => 'KALE'
            2 => 'CHARD'
            3 => 'SPINACH'
            4 => 'LETTUCE'
        ]
        'root' => [
            0 => 'RADISH'
            1 => 'TURNIP'
            2 => 'POTATO'
            3 => 'BEET'
        ]
        'other' => [
            0 => 'BROCOLLI'
            1 => 'GREEN BEANS'
            2 => 'CORN'
            3 => 'TOMATOES'
        ]
    ]
    'grains' => [
        0 => 'WHEAT'
        1 => 'RICE'
        2 => 'OATS'
    ]
]
Junaid Atari
  • 533
  • 7
  • 17
0

Hi, try this solution. Simple use php array map

function myfunction($value)
{
 return strtolower($value);
}

$new_array = ["Value1","Value2","Value3" ];
print_r(array_map("myfunction",$new_array ));

Output Array ( [0] => value1 [1] => value2 [2] => value3 )

0

Target specific values with array_map

$myArray = $_POST['product_categories']

$lowercaseArray = array_map(function ($value){
      if(isset($value['title'])) {
          $value['title'] = trim(strtolower($value['title']));
      }
      return $value;
  }, $myArray);
Ngatia Frankline
  • 2,897
  • 2
  • 20
  • 19
-3

You can also use a combination of array_flip() and array_change_key_case(). See this post

Jasir
  • 677
  • 1
  • 8
  • 26
  • 5
    Array data is lost after calling `array_flip()` if array contains duplicated values. – Pang Oct 25 '14 at 01:23