Skip to content
Related Articles
Open in App
Not now

Related Articles

Converting all keys into snake case in multidimensional array in PHP

Improve Article
Save Article
  • Difficulty Level : Expert
  • Last Updated : 17 Jan, 2023
Improve Article
Save Article

In the Snake case, we need to convert a given sentence or a word to a style of writing where all upper case characters will be replaced with lower case and all the gaps will be replaced with an underscore(_). In this article, we will see how to find the snake case of the all keys of a given array.

Here, we need to check each of the elements present in the array and then convert it to the snake case. There is one possibility that the array may contain some values whose snake case will be the same as it is. To solve this problem, two approaches are discussed below:

Approach 1: In the first approach, a function is declared where the logic for snake case conversion is defined. This function is responsible for all conversion that is upper to lower case and filling the gaps between letters by an underscore(_). Then, this function is called for each and every element present in the array. This is a recursive method to check whether the elements are already in snake case or not. If they are already in snake case then it will be as it is, else it will be converted to snake case and printed.

Example 1: This example illustrates the above approach for converting all keys into a snake case in a multidimensional array.


// Input array
foreach ([
    'geek' => 'geek',
    'Geek' => 'Geek',
    'GeeksforGeeks' => 'GeeksforGeeks',
    'Geeks4Geeks' => 'Geeks4Geeks',
    'GeekGeekGeek' => 'GeekGeekGeek',
    'geek_geek' => 'geek_geek',
    'Geek4' => 'Geek4',
    'It is Computer Science Portal' => 'It is a Computer Science Website',
    'Technical Scripter 2022' => 'Technical Scripter',
    ] as $case => $finalvalue) {
        $out = arrayInput($case);
    if ($out === $finalvalue) {
        echo "$case => [$finalvalue]\n";
    } else {
        echo "$case => [$out]\n";
function arrayInput($in) {
    $logic = '!([A-Z][A-Z0-9]*(?=$|[A-Z][a-z0-9])|[A-Za-z][a-z0-9]+)!';
    preg_match_all($logic, $in, $similar);
    $x = $similar[0];
    foreach ($x as &$similar) {
        $similar = $similar == strtoupper($similar) ?
            strtolower($similar) :
    return implode('_', $x);


geek => [geek]
Geek => [geek]
GeeksforGeeks => [geeksfor_geeks]
Geeks4Geeks => [geeks4_geeks]
GeekGeekGeek => [geek_geek_geek]
geek_geek => [geek_geek]
Geek4 => [geek4]
It is Computer Science Portal => [it_is_computer_science_portal]
Technical Scripter 2022 => [technical_scripter]

Approach 2: In the second approach, a class is defined where all the array values are declared as its object. It is a little tricky here that all the objects need to be declared. After that, the input array will be generated from these objects. Finally, one function is declared to key generation and append the conversion logic. Similarly, all the values will be checked and then converted to a snake case if needed and printed. This is an object-oriented approach where the values of the input array are considered as objects initially. There may be some warning in output but that can be ignored simply.

Example 2: This example illustrates the above approach for converting all keys into a snake case in a multidimensional array.


class GFG {
    public $GeeksForGeeks;
    public $GFGevent;
class Events {
    public $TechnicalScripter2022;
    public $codingChallenge;
    public $SDEhiringEvent;
$GFG = new GFG();
$GFG->GeeksForGeeks = 'GeeksForGeeks';
$GFG->GFGevent->TechnicalScripter2022 = 'TechnicalScripter2022';
$GFG->GFGevent->codingChallenge = 'codingChallenge';
$GFG->GFGevent->SDEhiringEvent= 'SDEhiringEvent';
$arrtrasformed = arrayInput($GFG);
function arrayInput($x){
    // Transforming array from an object
    $array = json_decode(json_encode((array) $x),true);
    // Transforming all array keys
    return keyTrans($array);
function keyTrans($array) {
    foreach ($array as $key => $value){
        // Assuming array as keys and comparing values
        unset($array[$key]); // Unsetting keys
        // Conversion logic
        $keyTrans = strtolower(preg_replace('/([a-z])([A-Z])/',
                                      '$1_$2', ltrim($key, '!')));
        $array[$keyTrans] = $value
        if (is_array($value)) {
            $array[$keyTrans] = keyTrans($value);
    return $array;


PHP Warning:Creating default object from empty value in /home/main.php on line 16
   [geeks_for_geeks] => GeeksForGeeks
   [gfgevent] => Array
           [technical_scripter2022] => TechnicalScripter2022
           [coding_challenge] => codingChallenge
           [sdehiring_event] => SDEhiringEvent

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!