PHP recursive JSON class instance, PHP recursive json_php tutorial

Source: Internet
Author: User
Tags php programming

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 ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.