Skip to content

Instantly share code, notes, and snippets.

@markalanevans
Created October 21, 2011 22:55
Show Gist options
  • Save markalanevans/1305207 to your computer and use it in GitHub Desktop.
Save markalanevans/1305207 to your computer and use it in GitHub Desktop.
LowerCase and Underscore PHP array
function underscore_array_keys($data){
foreach ($data as $n => $v){
$newKey = strtolower(preg_replace('/(?<=\\w)([A-Z])/', '_\\1', $n));
unset($data[$n]);
if (is_array($v)){
$data[$newKey] = underscore_array_keys($v);
}else{
$data[$newKey] = $v;
}
}
return $data;
}
Converts: Array( "User"=>array("id" => "123", "FirstName"=>"Mark"));
To: Array( "user"=>array("id" => "123", "first_name"=>"Mark"));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment