How to Convert an Array of Strings to Lowercase in PHP?

Find out how to make all strings lowercase in a PHP array of strings

In PHP, you can make all strings in an array of strings lowercase by calling the strtolower() function on every element of the array using array_map(), for example, like so:

// PHP 7.4+
$arr = ['FOO', 'Bar', 'bAz'];
$newArr = array_map(fn ($str) => strtolower($str), $arr);

var_dump($newArr); // ['foo', 'bar', 'baz']

You can rewrite the callback to array_map() without arrow function to make it compatible with earlier versions of PHP.

The code above would create a new array with all strings in the array in lowercase. You can achieve the same with a simple for loop as well:

$arr = ['FOO', 'Bar', 'bAz'];
$newArr = [];

foreach ($arr as $str) {
    $newArr[] = strtolower($str);

var_dump($newArr); // ['foo', 'bar', 'baz']

Hope you found this post useful. It was published . Please show your love and support by sharing this post.