So, I wrote this functions. It automatically parse a whole json_decode() -> both as Object and as Array (see commentary).
I searched a lot on the web and didn't find this. It could be useful for others what do you think?
//$object=json_decode($json_string)
//PHP json_decode Parser. In this exemple, the Datas will be put in a string
function parse_json_object($object,$level=0){
$string="";
$level++;
$level_show=$level-1;
foreach($object as $key => $value)
{
$string.=str_repeat("- \t",$level_show).$key; //Do what you want with the Keys
if(is_string($value) || is_float($value) || is_int($value) || is_bool($value)){
$string.= " : ".$value." (level:".$level_show.")</br>\n"; //Do what you want with the values
}
elseif(is_array($value)){
$string.="[arr] : (level:".$space.")</br>\r";
$array_level=$level+1;
$array_level_show=$array_level-1;
foreach($value as $arrayKey => $arrayValue) {
$string.=str_repeat("- \t",$array_level_show)."$arrayKey : (level:".$array_level_show.")</br>\r"; //Do what you want with the arrayKeys
$string.=parse_json_object($arrayValue,$array_level); //use recurse to parse the the objects of the array
}
}
elseif(is_object($value)){
$string.="[obj] : (level:".$level_show.")</br>\r";
$string.=parse_json_object($value,$level); //use recurse to parse the objects of sub Hierarchie
}
}
return $string;
}
and
//$array=json_decode($json_string, TRUE)
//PHP json_decode Parser. In this exemple, the Datas will be put in a string
function parse_json_array($array,$level=0){
$string="";
$level++;
$level_show=$level-1;
foreach($array as $key => $value)
{
$string.=str_repeat("- \t",$level_show).$key; //Do what you want with the Keys
if(is_string($value) || is_float($value) || is_int($value) || is_bool($value)){
$string.=" : ".$value." (level:".$level_show.")</br>\n"; //Do what you want with the values
}
elseif(is_array($value)){
$string.="[] : (level:".$level_show.")</br>\r";
$string.=parse_json_array($value,$level); //use recurse to parse the subarrays
}
elseif(is_object($value)){
//that schould be not possible
}
}
return $string;
}
Cheers