Skip to content

Instantly share code, notes, and snippets.

@lesthack
Created July 19, 2012 17:00
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 lesthack/3145314 to your computer and use it in GitHub Desktop.
Save lesthack/3145314 to your computer and use it in GitHub Desktop.
parseArrayToJson
<?php
...
public function parseArrayToJSon($object){
if( $this->is_assoc($object) ){
$ctotal = count($object);
$ccount = 0;
$source = "";
foreach($object as $key=>$value){
$source .= "\"$key\": ";
if( !is_array($value) )
if( is_string($value) ) $source .= "\"$value\"";
else $source .= "$value";
else
$source .= $this->parseResponse($value);
if(++$ccount < $ctotal) $source .= ", ";
}
return "{ ".$source." }";
}
else{
$ctotal = count($object);
$ccount = 0;
$source = "";
foreach($object as $value){
if( !is_array($value) )
if( is_string($value) ) $source .= "\"$value\"";
else $source .= "$value";
else
$source .= $this->parseResponse($value);
if(++$ccount < $ctotal) $source .= ", ";
}
return "[ ".$source." ]";
}
}
private function is_assoc($array) {
return (is_array($array) && (count($array)==0 || 0 !== count(array_diff_key($array, array_keys(array_keys($array))) )));
}
...
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment