Home  >  Article  >  Backend Development  >  How to convert PHP array data to json

How to convert PHP array data to json

醉折花枝作酒筹
醉折花枝作酒筹Original
2021-04-06 15:55:194729browse

In PHP, you can use the json_encode() function to convert array data into json. The json_encode() function can json encode variables. If the conversion is successful, it will return json data, otherwise it will return false.

How to convert PHP array data to json

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

Now when we need to useajaxHow to convert php arrays to json when interacting with the background.

The powerful PHP has provided built-in functions: json_encode() and json_decode(). It is easy to understand that json_encode() converts a PHP array into Json. On the contrary, json_decode() is to convert Json into a PHP array.

The specific form of Json is:

1. Object

The object is an unordered "'name/value' pair" gather. An object starts with "{" (left bracket) and ends with "}" (right bracket). Each "name" is followed by a ":" (colon); "name/value" pairs are separated by a "," (comma).

2. Array

An array is an ordered collection of values ​​(value). An array starts with "[" (left bracket) and ends with "]" (right bracket). Values ​​are separated by "," (comma).

Note: The two forms of objects and arrays are called differently in JS. Objects are called with "." and arrays are called with subscripts [0] and [1]. Also note that when passing a Json string, values ​​of type string must be enclosed in quotes.

Instance 1:

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

The program will print out:

{“name”:”Eric”,”age”:23}

Instance 2:

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

The program will output:

["Eric",23]

It can be seen from the above two examples that the two calls in js are different. The keys of the PHP array are all numbers, then json_encode() returns Json in the form of an array, if the keys of the PHP array are all strings. Then json_encode() will return Json in the form of an object.

In fact, as long as there is a key in the form of a string in the key of the PHP array, then json_encode() will return Json in the form of an object. This is not correct. Because, although no errors will occur in the PHP code, if such a Json is passed to a JS function, JS will treat the Json as an object, and it is impossible for an object to use numbers as attribute names. In other words, JS does not know what this is: user.0.username (the middle is the number zero)

Recommended learning: php video tutorial

The above is the detailed content of How to convert PHP array data to json. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn