自己写了个低版本实现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>
其他<br>
{<br>
$str .= """.$sun."",";<br>
}<br>
}<br>
其他<br>
{<br>
$str .= """.$value."",";<br>
}<br>
}<br>
$str = "[".trim($str,",")."]";<br>
返回 $str;<br>
}<br>
// 一个检验批次不是严格的数字索引 <br>
公共函数 TypeArr($arr)<br>
{<br>
if(array_values($arr) === $arr) 返回 true;<br>
返回 false;<br>
}<br>
<br>
}<br>
?>
json.zip ( 713 B 下载:31次 )
AD:真正免费,域名虚机企业邮箱=0元