Home > php教程 > php手册 > 分享JSON类

分享JSON类

WBOY
Release: 2016-06-07 11:43:51
Original
1289 people have browsed it

自己写了个低版本实现JSON格式数据生成,但不知道对不对。2013-09-27修改上传
<?php <br /> /* <br>  * @ anthor:QD<br>  * @ time:  2013-09-27<br>  */<br> class json{<br> <br>     private $Arr = array();    //传入数组<br>     <br>     //构造器<br>     public function json($array)<br>     {<br>         if(!is_array($array)) return false;<br>         $this->Arr = $array;<br>     }<br>     //解析主函数<br>     public function MainArr()<br>     {<br>         $arr = $this->Arr;<br>         if($this->TypeArr($arr))<br>         {<br>             $json = $this->NumArr($arr);<br>         }<br>         else<br>         {<br>             $json = $this->IndexArr($arr);<br>         }<br>         return $json;<br>         <br>     }<br>     //解析索引数组<br>     public function IndexArr($arr)<br>     {<br>         $str ="";<br>         foreach($arr as $k=>$value)<br>         {<br>             if(is_array($value))<br>             {<br>                 if($this->TypeArr($value)) { $sun=$this->NumArr($value);}<br>                 else               {$sun=$this->IndexArr($value);}<br>                 if(strpos($sun,"}") || strpos($sun,"]"))<br>                 {<br>                     $str .= "\"".$k."\":".$sun.",";<br>                 }<br>                 else<br>                 {<br>                     $str .= "\"".$k."\":\"".$sun."\",";<br>                 }<br>             }<br>             else<br>             {<br>                  $str .= "\"".$k."\":\"".$value."\",";<br>             }<br>         }<br>         $str = "{".trim($str,",")."}";<br>         return $str;<br>     }<br>     //解析数字数组<br>     public function NumArr($arr) <br>     {<br>         $str = "";<br>         foreach($arr as $value)<br>         {<br>             if(is_array($value))<br>             {<br>                 if($this->TypeArr($value)) { $sun=$this->NumArr($value);}<br>                 else               {$sun=$this->IndexArr($value);}<br>                 if(strpos($sun,"}") || strpos($sun,"]"))<br>                 {<br>                     $str .= $sun.",";<br>                 }<br>                 else<br>                 {<br>                     $str .= "\"".$sun."\",";<br>                 }<br>             }<br>             else<br>             {<br>                 $str .= "\"".$value."\",";<br>             }<br>         }<br>         $str = "[".trim($str,",")."]";<br>         return $str;<br>     }<br>     //检验一个数组是不是严格数字索引   <br>     public function TypeArr($arr)<br>     {<br>         if(array_values($arr) === $arr) return true;<br>         return false;<br>     }<br>     <br> }<br> ?>

附件 json.zip ( 713 B 下载:31 次 )

AD:真正免费,域名+虚机+企业邮箱=0元

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template