First download the following file (this is a piece of code written by someone else to parse json), and then introduce this file!

Now when we need to interact with the background using ajax,How to transfer php array to js file and 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). "]";

json_encode () is to json encode each value of $arr, then we want to output an array of json, so we add a comma after each compiled value and finally add "[]" to all values This is the format of the json array,Note that because we add a comma to each value after json encoding,This results in an extra comma when the last value is merged into the array.So we need to remove its last comma with the substr () function!

Then we look at the js file

When we use arr to receive the json array transmitted by the php file

var json=json.parse (arr);

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

In fact, to put it bluntly, the idea of ​​converting php arrays into js arrays is to use json as an intermediate amount to achieve! Of course, you can also use php and js to implement array conversion.More than one method!

Conversion between php array and json

The reason to use json is often because of the data interaction between the program and the js function when using the ajax object.Because js doesn't recognize arrays in PHP,PHP doesn't recognize arrays or objects in js.json solves this problem well.

Introduction to json

json (javascript object notation) is a lightweight data exchange format.It is based on a subset of javascript,This means that javascript can read json directly, which is very convenient.

The specific form of json is:


The object is an unordered collection of "name/value" pairs. An object begins with "{" (left parenthesis) and ends with "}" (right parenthesis). Each "name" is followed by a ":" (colon);""/value "pairs are separated by", "(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>


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

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

Code example:

<script type="text/javascript">function getarray ()
var arr=["jarry", 23, ["www.xiaophper.com", "[email protected]"]];
alert (arr [0]);alert (arr [1]);alert (arr [2] [0]);
alert (arr [2] [1]);
getarray ();

Note:The two forms of object and array are not the same when called in JS.Objects are called with "." And arrays are called with subscripts [0], [1]. Also note that string values ​​must be enclosed in quotes when passing json strings.

Convert array to json in php

Powerful PHP already provides built-in functions:json_encode () and json_decode (). Easy to understand,json_encode () is to convert php array to json. Instead, json_decode () converts json into a php array.


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

The program will print:

{"Name":"eric", "age":23}

Look at the following example again:

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

The program will print:["eric", 23]

The above two examples can be seen,If the keys of the php array are all numbers,Then json_encode () returns an array of json if the keys of the php array are all strings.Then json_encode () will return json as an object. Just said.The calling in js is different.

In fact, as long as there is a string key in the keys of the php array,Then json_encode () will return json as an object. This is not correct.Because, although there will be no errors in the php code,But if you pass such json to js function, js will treat this json as an object,It is impossible for objects to use numbers as attribute names.In other words, js doesn't know what it is:user.0.username (the middle is the number zero)

  • Previous JS production with mask pop-up layer to achieve login registration form special effects code sharing
  • Next Java IO Stream Common API Examples of File Class