How to Convert Array Key to Lowercase in PHP?

By Hardik Savani May 14, 2024 Category : PHP

Whenever you require to create all array key in lowercase then you can create that without using loop. you have to just use array_change_key_case(), array_change_key_case function take two argument one for array and second one for php function "CASE_LOWER". May be we need to do this when we are working on big projects and need to all key in small case.

So, how to convert array values to lowercase using array_change_key_case(), see bellow example and learn how to work.

Example:

<?php

$myArray = ['Hey'=>'Hey','HELLO'=>'Hello','hi'=>'Hi','Gm'=>'GM'];

$result = array_change_key_case($myArray, CASE_LOWER);

print_r($result);

Output:

Array

Array

(

[hey] => Hey

[hello] => Hello

[hi] => Hi

[gm] => GM

)

I hope it can help you...

Tags :
Shares