Conversion between PHP arrays and Json

Source: Internet
Author: User

In php, array and json conversion are simple. We only need to use json_encode () and json_decode (). It is easy to understand that json_encode () is to convert the PHP array to Json. On the contrary, json_decode () converts Json to 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]

In this way, json can be converted into an array, and the key remains in the original format.

The Code is as follows: Copy code

$ Json = '{"name": "zhangsan", "age": 20, "sex": "nan "}';

Print_r (json_decode ($ json, true ));

After json data is parsed, it will become the following Array


Array
(
[Name] => zhangsan
[Age] => 20
[Sex] => nan
)

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.