An explanation of the conversion between PHP arrays and JSON _php tutorial

Source: Internet
Author: User
In PHP, arrays and JSON transformations are simple, as long as we use Json_encode () and Json_decode (). It is easy to understand that Json_encode () is the conversion of PHP arrays into JSON. Instead, Json_decode () converts the JSON into a PHP array.

For example:

The code is as follows Copy Code

$array = Array ("name" = "Eric", "age" = 23);

echo Json_encode ($array);

The program will print out:

{"Name": "Eric", "Age": 23}

Let's look at the following example:

The code is as follows Copy Code

$array = Array (0 = "Eric", 1 = 23);

echo Json_encode ($array);

The program will print out:

["Eric", 23]

This allows you to convert the JSON into an array form, and the key remains in its original format

The code is as follows Copy Code

$json = ' {' name ': ' Zhangsan ', ' age ': ', ' sex ': ' nan '} ';

Print_r (Json_decode ($json, true));

This JSON data is parsed and becomes an array like this


Array
(
[Name] = Zhangsan
[Age] = 20
[Sex] = Nan
)

http://www.bkjia.com/PHPjc/628751.html www.bkjia.com true http://www.bkjia.com/PHPjc/628751.html techarticle in PHP, arrays and JSON transformations are simple, as long as we use Json_encode () and Json_decode (). It is easy to understand that Json_encode () is the conversion of PHP arrays into JSON. On the contrary, Json_decode () is ...

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