Jump to content

revepastrop

New Members
  • Posts

    3
  • Joined

  • Last visited

    Never

Everything posted by revepastrop

  1. 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
  2. THANKS a lot ! Actually I didn't understood very well how recursiv Functions functions. Now with your correction PaulRyan and and your explanation MjDamato, I'm perfect ! Very well, Thanks a lot ! I don't close the topic because maybe I'll ask you about my real Function (Parsing a hierarchical JSON).
  3. Hello! Maybe I'm a bit ashamed to question some little kind of thing, but I tested 3 hours and couldn't let this function. It's like the most easiest possible recursiv function...: function recursiv($s){ $s.="b"; if(strlen($s)>30){return $s;} //else return 0; else recursiv($s); } echo recursiv("h"); The result is a blank page... Thanks a lot for your help !!
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.