自己写了个低版本实现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元