PHP recursive JSON class instance, PHP recursive JSON
This paper describes the implementation of PHP recursive JSON class. Share to everyone for your reference.
The specific implementation code is as follows:
Copy CodeThe code is as follows: <?php
/*
* @ anthor:qd
* @ time:2013-09-27
*/
Class json{
Private $ARR = Array (); Incoming array
Constructors
Public function json ($array)
{
if (!is_array ($array)) return false;
$this->arr = $array;
}
Parse main function
Public Function Mainarr ()
{
$arr = $this->arr;
if ($this->typearr ($arr))
{
$json = $this->numarr ($arr);
}
Else
{
$json = $this->indexarr ($arr);
}
return $json;
}
Parsing an indexed array
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;
}
Parsing a numeric array
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;
}
Verify that an array is not strictly numeric indexed
Public Function Typearr ($arr)
{
if (array_values ($arr) = = = $arr) return true;
return false;
}
}
?>
I hope this article is helpful to everyone's PHP programming.
http://www.bkjia.com/PHPjc/920607.html www.bkjia.com true http://www.bkjia.com/PHPjc/920607.html techarticle PHP Recursive JSON class instance, PHP recursive JSON This example describes the PHP recursive JSON class implementation method. Share to everyone for your reference. The implementation code is as follows: Copy code code as follows ...