php script on server side:
$data['id'] = 1;
$dat['name'] = "mary";
$da['red']= array_merge($data,$dat);
$data1['id'] = 2;
$dat1['name'] = "Swallow";
$da['blue']= array_merge($data1,$dat1);
print_r($da); //The printout is a two-dimensional array (as shown below)
/*
Array
(
[red] => Array
(
[id] => 1
[name] =>
) )
[blue] => Array
(
[id] => 2
[name] =>
) )
)
*/
echo json_encode($da);//The output is a string converted into json format, which can be used directly in js (as follows)
/*
{"red":{"id":1,"name":"mary"},"blue":{"id":2,"name":"u71d5u5b50"}}
*/
?>
jquery script:
Processing after returning to js:
The first one requires varl conversion: when it is a string, use eval to convert it into a jquery object (as shown below)
Copy code
The code is as follows:
var arr = '{"red":{"id":1,"name":"mary"},"blue":{"id":2,"name":"u71d5u5b50"}}'; //u71d5u5b50 This is automatically converted in php
var dataObj = eval("(" arr ")"); //I don’t know the reason why brackets and double quotes are added here. I just treat it as json syntax and can only memorize it by rote.
$.each(dataObj,function(idx,item){
//Output
alert(item.id "Haha" item.name);
})
The second type: does not require conversion:
Copy code
The code is as follows:
var arr = {"red":{"id":1,"name":"mary"},"blue":{"id":2,"name":"u71d5u5b50"}};
$.each(arr,function(idx,item){
//Output
alert(item.id "Haha" item.name);
})
There are also two methods of looping:
Copy code
$.each(arr,function(idx,item){
//Output
alert(item.id "Haha" item.name);
})
//Method 2:
for(var key in arr){
alert(key);
alert(arr[key].status);
}
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