|
以下是三零網(wǎng)為大家整理的最新php遞歸json類分享的文章,希望大家能夠喜歡!在這里為大家?guī)硪黄猵hp遞歸json 類分享。希望對您的學(xué)習(xí)PHP有所幫助。 具體實現(xiàn)代碼如下: <?php class json{ private $Arr = array(); //傳入數(shù)組 //構(gòu)造器 public function json($array) { if(!is_array($array)) return false; $this->Arr = $array; } //解析主函數(shù) public function MainArr() { $arr = $this->Arr; if($this->TypeArr($arr)) { $json = $this->NumArr($arr); } else { $json = $this->IndexArr($arr); } return $json; } //解析索引數(shù)組 public function IndexArr($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; } //解析數(shù)字?jǐn)?shù)組 public function NumArr($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; } //檢驗一個數(shù)組是不是嚴(yán)格數(shù)字索引public function TypeArr($arr) { if(array_values($arr) === $arr) return true; return false; } } ?>
信息發(fā)布:廣州名易軟件有限公司 http://m.jetlc.com
|