Home >Backend Development >PHP Tutorial >PHP recursive json class example, PHP recursive json_PHP tutorial

PHP recursive json class example, PHP recursive json_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:12:41969browse

php recursive json class instance, php recursive json

The example in this article describes the implementation method of PHP recursive json class. Share it with everyone for your reference.

The specific implementation code is as follows:

Copy code The code is as follows:
/*
* @anthor:QD
* @ time: 2013-09-27
*/
class json{
private $Arr = array(); //Pass in array
//Constructor
public function json($array)
{
if(!is_array($array)) return false;
$this->Arr = $array;
}
//Analyze the main function
public function MainArr()
{
$arr = $this->Arr;
if($this->TypeArr($arr))
{
$json = $this->NumArr($arr);
}
else
{
$json = $this->IndexArr($arr);
}
Return $json;
}
//Parse the index array
public function IndexArr($arr)
{
$str ="";
foreach($arr as $k=>$value)
{
if(is_array($value))
{
If($this->TypeArr($value)) { $sun=$this->NumArr($value);}
else 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;
}
//Parse the numeric array
public function NumArr($arr)
{
$str = "";
foreach($arr as $value)
{
if(is_array($value))
{
If($this->TypeArr($value)) { $sun=$this->NumArr($value);}
else else {$sun=$this->IndexArr($value);}
If(strpos($sun,"}") || strpos($sun,"]"))
{
$str .= $sun.",";
}
else
{
$str .= """.$sun."",";
}
}
else
{
$str .= """.$value."",";
}
}
$str = "[".trim($str,",")."]";
Return $str;
}
//Check whether an array is strictly numerically indexed
public function TypeArr($arr)
{
if(array_values($arr) === $arr) return true;
return false;
}
}
?>

I hope this article will be helpful to everyone’s PHP programming design.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/920607.htmlTechArticlephp recursive json class example, php recursive json This article describes the implementation method of php recursive json class. Share it with everyone for your reference. The specific implementation code is as follows: Copy the code The code is as follows...
Statement:
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