PHP多维数组的jQuery [英] php multidimensional array into jQuery
本文介绍了PHP多维数组的jQuery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我用下面的code从表中填充一个php多维数组:
I have a php multidimensional array populated from a table using the following code:
<?php starts
$array = array();
$i = 0;
while($row = mysql_fetch_array($result))
{
$array[$i] = array("handle" => $row['handle'],"firstname" => $row['first_name'],"lastname" => $row['last_name']);
$i++;
}
echo json_encode(json_encode($array));
?> php ends
这是由来自.post的jQuery的调用,当我提醒()返回的数据我得到以下的输出:
this is called by .post from jQuery and when I alert() the data returned I get the following output:
[
{"handle":"admin","firstname":"admin","lastname":"admin"},
{"handle":"ms","firstname":"ms","lastname":"ms"},
{"handle":"op","firstname":"op","lastname":"op"},
{"handle":"ui","firstname":"ui","lastname":"ui"}
]
Jquery的code我用提取PHP数组是:
the Jquery code I use to extract the php array is:
$.post("test1.php","",
function(data){
var obj = $.parseJSON(data);
alert(obj);
var obj2 = $.parseJSON(obj);
alert(obj2);
alert(obj2[1]);
var result = eval(data);
alert(result[0][0]);
},"html");
警报(OBJ)给我指定的输出。
警报(OBJ2)给我:
alert(obj) gives me the output specified. alert(obj2) gives me:
[object Object],[object Object],[object Object],[object Object]
警报(OBJ2 [1])给我:
alert(obj2[1]) gives me:
[object Object]
我如何在数据获得此???
How do I get at the data in this???
推荐答案
您第一个
var obj = $.parseJSON(data);
应该是你所需要的。然后,您可以访问喜欢你的对象:
Should be all you need. You can then access your objects like:
obj[0]['handle']
// or
obj[0].handle
这篇关于PHP多维数组的jQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文