PHP recursive JSON class instance code

Source: Internet
Author: User

This article mainly introduces the implementation method of PHP recursive JSON class, can realize the parsing of index array and numeric array, and then realize the function of recursive array to return JSON string.
The specific implementation code is as follows:

<?PHP/** @ anthor:qd * @ time:2013-09-27*/ classjson{Private $ARR=Array();//incoming Array//constructor  Public functionJson$array)  {   if(!Is_array($array))return false; $this->arr =$array; }  //Parse main function  Public functionMainarr () {$arr=$this-ARR; if($this->typearr ($arr))   {    $json=$this->numarr ($arr); }   Else   {    $json=$this->indexarr ($arr); }   return $json; }  //parsing an indexed array  Public functionIndexarr ($arr)  {   $str=""; foreach($arr  as $k=$value)   {    if(Is_array($value))    {     if($this->typearr ($value)) {$sun=$this->numarr ($value);} Else{$sun=$this->indexarr ($value);} if(Strpos($sun,"}") ||Strpos($sun,"]"))     {      $str.= """.$k."":".$sun.","; }     Else     {      $str.= """.$k."":"".$sun."","; }    }    Else    {      $str.= """.$k."":"".$value."","; }   }   $str= "{".Trim($str,",")."}"; return $str; }  //parsing a numeric array  Public functionNumarr ($arr)   {   $str= ""; foreach($arr  as $value)   {    if(Is_array($value))    {     if($this->typearr ($value)) {$sun=$this->numarr ($value);} Else{$sun=$this->indexarr ($value);} if(Strpos($sun,"}") ||Strpos($sun,"]"))     {      $str.=$sun.","; }     Else     {      $str.= """.$sun."","; }    }    Else    {     $str.= """.$value."","; }   }   $str= "[".Trim($str,",")."]"; return $str; }  //Verify that an array is not strictly numeric indexed  Public functionTypearr ($arr)  {   if(array_values($arr) ===$arr)return true; return false; } } ?>

PHP recursive JSON class instance code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.