php 递归json的类代码
发布:smiling 来源: PHP粉丝网 添加日期:2014-06-26 08:36:24 浏览: 评论:
- <?php
-
-
-
-
- class json{
-
- private $Arr = array();
-
-
- public function json($array)
- {
- if(!is_array($array)) return false;
- $this->Arr = $array;
- }
-
- public function MainArr()
- {
- $arr = $this->Arr;
- if($this->TypeArr($arr))
- {
- $json = $this->NumArr($arr);
- }
- else
- {
- $json = $this->IndexArr($arr);
- }
- return $json;
-
- }
-
- 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;
- }
-
- 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;
- }
-
- public function TypeArr($arr)
- {
- if(array_values($arr) === $arr) return true;
- return false;
- }
-
- }
- ?>
分享到:
相关文章
- ·PHP多功能图片处理类(2013-11-11)
- ·PHP 生成缩略图的类(2013-11-13)
- ·一个分页显示类(2013-11-13)
- ·分享的一个分页类(2013-11-13)
- ·简单的php分页类(2013-11-14)
- ·一个功能比较高的分页类(for PHP5.x)(2013-11-28)
- ·phpword中文字符乱码解决办法(2013-12-05)
- ·一个比较完善的购物车类(2013-12-08)
- ·php面象对象数据库操作类(2013-12-09)
- ·PHP顶层类(2013-12-10)
- ·PHP静态文件生成类(2013-12-10)
- ·PHP分页类(2013-12-10)
- ·非常简单的日历类(2013-12-11)
- ·php数字分页类的代码(2013-12-23)
- ·PHP货币换算程序代码(2013-12-27)
- ·实现多文件上传php类(2014-01-03)