Change language

How do I convert array values ​​to lowercase in PHP?

An array is given containing uppercase lowercase elements, and the task is to convert the array elements (uppercase) to lowercase. There are two ways to convert array values ​​to lowercase in PHP.
  • Using the foreach loop
  • Using the array_map()
Using the foreach loop :in this approach, the iterator iterates over the value of the array individually, converts each element of the array to lowercase, and stores the converted values ​​in the original array.Program:  
// Declare the array $arr = array ( ’GFG’ , ’GEEK’ , ’GEEKS’ , ’ GEEKSFORGEEKS’ ); $j = 0;  
// Loop iterate to transform the array // lowercase elements and
// overwrite the original array foreach ( $arr as $element ) { $arr [ $j ] = strtolower ( $element ); $j ++; }  
// Display the contents of the array foreach ( $arr as $element ) echo $element . "" ;  
?>
Exit:
gfg geek engineer engineerforengineer
Using array_map():In this approach, array_map() is used to accept two callback and array parameters. 
Syntax:
array array_map (callable callback, array array)
Here’s a callback - this is the function that is called to work with the array. This function can be a built-in or user-defined function, whereas the array - this is a list of values ​​to operate on.Program 2:  
// Declare the array $arr = array ( ’GFG’ , ’ GEEK’ , ’GEEKS’ , ’ GEEKSFORGEEKS’ ); $arr = array_map ( ’ strtolower’ , $arr );  
// Show array contents foreach ( $arr as $element ) echo $element . "" ;  
?>
Exit:
gfg geek engineer engineerforengineer

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers


Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method