JSON usage: Convert PHP array to JS array, how JS receives PHP array, jsonjs_PHP tutorial

WBOY
Release: 2016-07-12 09:07:53
Original
1128 people have browsed it

JSON usage: Convert PHP array to JS array, how to receive PHP array in JS, jsonjs

First download the following file (this is a piece of code written by others to specifically parse json ), and then import this file!

http://pan.baidu.com/s/1dD8qVr7

Now when we need to use ajax to interact with the background, how to transfer the php array to the js file and have it recognized by js?

First look at the php file, when we get the $arr array

foreach ($arr as $value) {
  $json .= json_encode($value) . ',';
} 
echo '[' . substr($json,0,strlen($json) - 1) . ']';
Copy after login

json_encode() is to json encode each value of $arr, and then we want to output a json array, so we add a comma after each compiled value and finally add '[ outside all values ]', this is the format of the json array. Note that because we add a comma after each value is json encoded, this will result in an extra comma when all values ​​are merged into the array, so we have to use substr() The function removes its last comma!

Then let’s look at the js file

When we use arr to receive the json array of php file transfer

var json = JSON.parse(arr);
Copy after login

JSON is an object defined in the file we started downloading. We use its parse method to convert the json array into a js array! This is the variable json that receives a js array, so it cannot be printed directly. You can traverse the json array or json[0] to output!

Actually, to put it bluntly, our idea of ​​converting a php array into a js array is to use the intermediate quantity of json to achieve it! Of course, you can also use only php and js to convert the array, there is more than one method!

Conversion between PHP array and Json

The reason why Json is used is often because of the data interaction between the program and the JS function when using the ajax object. Because JS does not recognize arrays in PHP, PHP does not recognize arrays or objects in JS. Json solves this problem very well.

Json Introduction

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

The specific form of

Json is:

1. Object

An object is an unordered collection of "name/value" pairs. 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).

For example: {“username”: “Eric”,”age”:23,”sex”: “man”}

Code example:

<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>
Copy after login

2. Array

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

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

Code example:

<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>
Copy after login

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 string type values ​​must be enclosed in quotes when passing Json strings.

Convert array to Json in PHP

The powerful PHP already provides 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() converts Json into a PHP array.

For example:

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

The program will print out:

Copy code The code is as follows:
{"name":"Eric","age":23}

Look at the following example:

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

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

As can be seen from the above two examples, if 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. Just said it. The two calls in js are different.

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 incorrect. 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 number zero in the middle)

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1058150.htmlTechArticleJSON usage: Convert PHP array to JS array, how does JS receive PHP array, jsonjs First download the following file (this It is a piece of code written by someone else to specifically parse json), and then introduce this...
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template