Thursday, November 13, 2025
HomeLanguagesHow to convert array values to lowercase in PHP ?

How to convert array values to lowercase in PHP ?

Given an array containing uppercase string elements and the task is to convert the array elements (uppercase) into lowercase. There are two ways to convert array values to lowercase in PHP. 
 

Using foreach loop: In this approach, an iterator iterates through the value of an array individually and convert each array elements into lowercase and stores the converted values to the original array.
Program: 
 

PHP




<?php
 
// Declare an array
$arr = array('GFG', 'GEEK',
        'GEEKS', 'GEEKSFORGEEKS');
 
$j = 0;
 
// Iterate loop to convert array
// elements into lowercase and
// overwriting the original array
foreach( $arr as $element ) {
    $arr[$j] = strtolower($element);
     
    $j++;
}
 
// Display the content of array
foreach( $arr as $element )
    echo $element . "\n";
 
?>


Output: 

gfg
geek
neveropen
neveropen

 

Using array_map() function: In this approach, the array_map() function is used to accept two parameters callback and an array. 
Syntax: 
 

array array_map( callable callback, array array )

Here callback is a function to be called for operation on an array. This function can be an inbuilt function or an user-defined function whereas array is list of values on which operation to be performed.
Program 2: 
 

PHP




<?php
 
// Declare an array
$arr = array('GFG', 'GEEK',
        'GEEKS', 'GEEKSFORGEEKS');
 
$arr = array_map( 'strtolower', $arr );
 
// Display the content of array
foreach( $arr as $element )
    echo $element . "\n";
 
?>


Output: 

gfg
geek
neveropen
neveropen

 

RELATED ARTICLES

Most Popular

Dominic
32399 POSTS0 COMMENTS
Milvus
95 POSTS0 COMMENTS
Nango Kala
6765 POSTS0 COMMENTS
Nicole Veronica
11916 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11983 POSTS0 COMMENTS
Shaida Kate Naidoo
6889 POSTS0 COMMENTS
Ted Musemwa
7141 POSTS0 COMMENTS
Thapelo Manthata
6835 POSTS0 COMMENTS
Umr Jansen
6839 POSTS0 COMMENTS