我有一个在PHP页面中看起来像这样的数组:
$page["1"] = array("element1","element2","element3","element4");
$page["2"] = array("element1","element2","element3","element4");
$page["3"] = array("element1","element2","element3","element4");
我需要在JAVASCRIPT文件(JQUERY)中从这个数组中检索一些数据。
如何导入例如elements4 $page[1][3],$page[2][3],$page[3][3]
?
我在这里看过很多JSON的例子但不喜欢这个......
答案 0 :(得分:0)
可能这可以帮到你(文件名:getdata.php):
<?
if($_GET['ajax']==1){
$page = array(
array("element1","element2","element3","element4"),
array("element1","element2","element3","element4"),
array("element1","element2","element3","element4"),
);
echo json_encode($page);
}else{
?>
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
<button class="getdata" >Get Data</button><div class="jsdata"></div>
</body>
<script>
$('.getdata').click(function(){
$.ajax({
type:"POST",
url:"getdata.php?ajax=1",
success:function(data){
var res = eval("("+data+")");
$('.jsdata').html("$page[1][3],$page[2][3],$page[3][3] respectively:"+res[0][3]+","+res[1][3]+","+res[2][3]);
}
})
});
</script>
</html>
<? } ?>