Conversion between PHP arrays and JSON

Source: Internet
Author: User
Tags php array to json

JSON is often used because of data interaction between programs and JS functions when Ajax objects are used. Because JS does not know arrays in PHP, PHP does not know arrays or objects in Js. JSON solves this problem.

Introduction to JSON

JSON (JavaScript Object Notation) is a lightweight data exchange format. It is based on a subset of JavaScript, which means javascript can directly read JSON, which is very convenient.

The JSON format is as follows:
1.Object

An object is an unordered set of 'name/value' pairs. An object starts with "{" (left parenthesis) and ends with "}" (right Parenthesis. Each "name" is followed by a ":" (colon); "," (comma) is used to separate the "name/value" pairs.

Example: {"username": "Eric", "age": 23, "sex": "man "}

Sample Code:

? View code Javascript

<script type="text/javascript">   function getUser(){    var   user = {    "username": "Eric",    "age":23,    "family": {"mother":"Marry","father":"Alon","brother":"Tom"}    };   alert( user.username );   alert( user.age );   alert(user.family.brother);}   getUser();   </script>

2,Array

An array is an ordered set of values. An array starts with "[" (left square brackets) and ends with "]" (right square brackets. Values are separated by commas.

Example: ["Eric", 23, "man"]

Sample Code:

? View code PHP

<script type="text/javascript">   function getArray()   {   var arr = ["Jarry",23, ["www.xiaophper.com","wxyh_999@126.com"]];   alert(arr[0]);   alert(arr[1]);   alert(arr[2][0]);   alert(arr[2][1]);   }   getArray();   </script>

Note: objects and arrays are called differently in JS. objects are called with "." And arrays are called with subscripts [0] and [1. Note that the value of the string type must be enclosed in quotation marks when a JSON string is passed.

Convert arrays to JSON in PHP

Powerful php provides built-in functions: 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:

? View code PHP

$array = array("name" => "Eric","age" => 23);   echo json_encode($array);

The program will print: {"name": "Eric", "age": 23}

Let's look at the following example:

? View code PHP

$array = array(0 => "Eric", 1 => 23);   echo json_encode($array);

The program will print out: ["Eric", 23]

The above two examples show that if the keys of the PHP array are numbers, then json_encode () returns an array of JSON, if the keys of the PHP array are all strings. Then json_encode () returns an object-type JSON. I already said. The two are called differently in Js.

In fact, as long as there is a string key in the PHP array key, then json_encode () will return the JSON in the object format. This is incorrect. Because, although PHP code does not produce errors, if such JSON is passed to the JS function, JS regards this JSON as an object, however, objects cannot use numbers as attribute names. That is to say, JS does not know what this is: user.0.username (in the middle is the number 0)

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.