Skip to content

Instantly share code, notes, and snippets.

@goldsky
Created August 16, 2012 18:36
Show Gist options
  • Star 17 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save goldsky/3372487 to your computer and use it in GitHub Desktop.
Save goldsky/3372487 to your computer and use it in GitHub Desktop.
Convert under_score type array's keys to camelCase type array's keys and likewise
<?php
/**
* Convert under_score type array's keys to camelCase type array's keys
* @param array $array array to convert
* @param array $arrayHolder parent array holder for recursive array
* @return array camelCase array
*/
public function camelCaseKeys($array, $arrayHolder = array()) {
$camelCaseArray = !empty($arrayHolder) ? $arrayHolder : array();
foreach ($array as $key => $val) {
$newKey = @explode('_', $key);
array_walk($newKey, create_function('&$v', '$v = ucwords($v);'));
$newKey = @implode('', $newKey);
$newKey{0} = strtolower($newKey{0});
if (!is_array($val)) {
$camelCaseArray[$newKey] = $val;
} else {
$camelCaseArray[$newKey] = $this->camelCaseKeys($val, $camelCaseArray[$newKey]);
}
}
return $camelCaseArray;
}
/**
* Convert camelCase type array's keys to under_score+lowercase type array's keys
* @param array $array array to convert
* @param array $arrayHolder parent array holder for recursive array
* @return array under_score array
*/
public function underscoreKeys($array, $arrayHolder = array()) {
$underscoreArray = !empty($arrayHolder) ? $arrayHolder : array();
foreach ($array as $key => $val) {
$newKey = preg_replace('/[A-Z]/', '_$0', $key);
$newKey = strtolower($newKey);
$newKey = ltrim($newKey, '_');
if (!is_array($val)) {
$underscoreArray[$newKey] = $val;
} else {
$underscoreArray[$newKey] = $this->underscoreKeys($val, $underscoreArray[$newKey]);
}
}
return $underscoreArray;
}
/**
* Convert camelCase type array's values to under_score+lowercase type array's values
* @param mixed $mixed array|string to convert
* @param array $arrayHolder parent array holder for recursive array
* @return mixed under_score array|string
*/
public function underscoreValues($mixed, $arrayHolder = array()) {
$underscoreArray = !empty($arrayHolder) ? $arrayHolder : array();
if (!is_array($mixed)) {
$newVal = preg_replace('/[A-Z]/', '_$0', $mixed);
$newVal = strtolower($newVal);
$newVal = ltrim($newVal, '_');
return $newVal;
} else {
foreach ($mixed as $key => $val) {
$underscoreArray[$key] = $this->underscoreValues($val, $underscoreArray[$key]);
}
return $underscoreArray;
}
}
@kpheasey
Copy link

Great help, thanks!

@dobidobz
Copy link

I agree. Nice work. Thanks!

@calonzolg
Copy link

Nice Work!

@nscheuner
Copy link

Hi, Function create_function() is deprecated in PHP7.2 do you plan updating your code?
Regards

@nscheuner
Copy link

nscheuner commented Nov 27, 2018

Would sugget replacing:
array_walk($new_key, create_function('&$v', '$v = ucwords($v);'));
With

array_walk($new_key, function (&$v){ $v = ucwords($v); });

@beauraines
Copy link

This is quite nice! Thanks

@rpujakesuma
Copy link

Would sugget replacing:
array_walk($new_key, create_function('&$v', '$v = ucwords($v);'));
With

array_walk($new_key, function (&$v){ $v = ucwords($v); });

Many Thanks @nscheuner

@flads
Copy link

flads commented Jan 4, 2021

This function that worked for me:
(and works with array of arrays)

function camelCaseKeys($array)
{
    foreach($array as $key => $value) {
        if (is_array($value)) {
            foreach($value as $x => $y) {
                $camelCase = str_replace(' ', '', ucwords(str_replace('_', ' ', $x)));
                $camelCase[0] = strtolower($camelCase[0]);
                $array[$key][$camelCase] = $y;
                unset($array[$key][$x]);
            }
        } else {
            $keyCamelCase = str_replace(' ', '', ucwords(str_replace('_', ' ', $key)));
            $keyCamelCase[0] = strtolower($keyCamelCase[0]);
            $array[$keyCamelCase] = $value;
            unset($array[$key]);
        }
    }

    return $array;
}

@drewbaker
Copy link

drewbaker commented Jun 8, 2023

@flads your function is close, but ID becomes iD

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment