Conversions between PHP arrays and JSON Json_encode () and Json_decode ()

Source: Internet
Author: User

JSON is used in many cases because of the interaction between the program and the JS function when using an Ajax object. Because JS does not know the array in PHP, PHP does not know the array or object in JS. JSON is a good solution to this problem.

Introduction to JSON

JSON (JavaScript Object Notation) is a lightweight data interchange format. It is based on a subset of JavaScript, which means that JavaScript can read JSON directly and is very handy.

The specific form of JSON is:
1. Objects

An object is an unordered collection of "name/value pairs". An object starts with "{" (opening parenthesis) and "}" (the closing parenthesis) ends. Each "name" is followed by a ":" (colon); "' Name/value ' pair ' is separated by", "(comma).

For example: {"username": "Eric", "age": +, "sex": "Man"}

code example:

<script type= "Text/javascript" >  
function GetUser () { var user = { "username": "Eric", "age": "Family": {"mother": "Marry", "Father": "Alon", "Brother": "Tom"} }; alert (user.username); alert (user.age); alert (user.family.brother);} GetUser (); </script>

2. Arrays

An array is an ordered collection of values (value). An array begins with "[" (the left square bracket), and "]" (the right square bracket) ends. Use "," (comma) to separate values.

For example: ["Eric", "+", "man"]

code example:

<script type= "Text/javascript" >  
function GetArray () { var
Alert (arr[0]); Alert (arr[1]); Alert (arr[2][0]); Alert (arr[2][1]); } GetArray ();
</script>

Note: Objects and arrays are not the same as the two forms in JS, the object with "." Called, the array is called with subscript [0], [1]. Also note that string-type values are enclosed in quotation marks when the JSON string is passed.

converting arrays into JSON in PHP
Powerful PHP already provides built-in functions: 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:

$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:

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

The program will print out: ["Eric", 23]
As you can see from the two examples above, if the keys of the PHP array are numbers, then Json_encode () returns a JSON in the form of an array, if the keys of the PHP array are all strings. Then Json_encode () returns a JSON in the form of an object. Just already said. The invocation of the two in JS is different.
In fact, as long as there is a string form key in the PHP array's key, then Json_encode () returns the JSON in the form of the object. This is not true. Because, although there is no error in PHP code, if you pass such a JSON to the JS function, JS will use this JSON as an object, and the object is not possible to use a number as the property name. That is to say JS do not know what this is: User.0.username (the middle is the number 0)

Conversions between PHP arrays and JSON Json_encode () and Json_decode ()

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.