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:
Object
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>
2.Array
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 ();
</script>
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.
E.g:
$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)
Related articles
- Method for converting array in JS to JSON format string
- Javascript object json array json string mutual conversion and value method
- JS to json format string as an object or array (front and back)
- Methods to use JSON and restore json to array in PHP language
- Simple implementation of json object and array and conversion into js object
- JS simple loop through the json array method
- How to add json data to js array and the difference between js array and json
- 3 ways for JQuery to traverse json array
- Detailed operation of javascript on arrays and json arrays
- php - coincheck api authentication doesn't work
- php - i would like to introduce the coincheck api so that i can make payments with bitcoin on my ec site
- [php] i want to get account information using coincheck api
- python - you may need to restart the kernel to use updated packages error
- the emulator process for avd pixel_2_api_29 was killed occurred when the android studio emulator was started, so i would like to
- python 3x - typeerror: 'method' object is not subscriptable
- javascript - how to check if an element exists in puppeteer
- xcode - pod install [!] no `podfile 'found in the project directory
- i want to call a child component method from a parent in vuejs
- vuejs - [vuetify] unable to locate target [data-app] i want to unit test to avoid warning