Skip to content

Instantly share code, notes, and snippets.

@prufrock
Created May 22, 2012 21:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save prufrock/2771834 to your computer and use it in GitHub Desktop.
Save prufrock/2771834 to your computer and use it in GitHub Desktop.
Makes XML and JSON play nice when being rendered from the same associate array.
<?php
function removeSingularKeys($array, $parent="")
{
$result = array();
$result = $array;
foreach($array as $key => $value){
if(keyIsTheSingularFormOf($key, $parent)){
return $value;
}
if(is_array($value)){
$result[$key] = removeSingularKeys($value, $key);
}
}
return $result;
}
function keyIsTheSingularFormOf($key, $parent){
if($parent == $key . "s"){
return TRUE;
}
return FALSE;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment