如何使用jQuery与来自PHP的信息数组 [英] how to use jquery with array of information coming from php
本文介绍了如何使用jQuery与来自PHP的信息数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在寻找一种方法来正确读取来自php脚本的数组:
I'm looking for a way to properly read an array coming from a php script:
php:
$query="SELECT *, UNIX_TIMESTAMP(TIME) as epoch_time FROM node WHERE netid='$netid'";
$result=mysql_query($query, $conn);
// Plot our nodes
while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
$longitude=$row["longitude"];
$latitude=$row["latitude"];
$points[] = array('lat' => $latitude, 'lng' => $longitude);
}
mysql_close($conn);
print_r ($points);
在单独调用时的脚本结果:
the result of script when called in standalone:
Array ( [0] => Array ( [lat] => 35.91204361476439 [lng] => 39.05123084783554 ) [1] => Array ( [lat] => 36.91204361476439 [lng] => 38.05123084783554 ) [2] => Array ( [lat] => 33.33 [lng] => 34.44 ) [3] => Array ( [lat] => 35.78 [lng] => 33.2 ) [4] => Array ( [lat] => 37.000000 [lng] => 32.3456 ) )
现在我想通过ajax管理此数组,获取数组中每个元素的经度对,我有以下想法:
Now I want to manage by ajax this array, getting the pair of latitude and longitude for each element in the array I have the following idea:
function loadPoints(){
$.ajax({
url:'markers.php',
success:function(points){
var markers = {};
markers = points;
for (var i in markers) {
$.each(markers[i], function(key, val){
var position = [val.lat, val.lng];
console.log(position);
});
}
}
});
,但是它不起作用.知道如何管理该阵列吗?
but it is not working. Any idea How can I manage this array?
谢谢
推荐答案
使用 json_encode 在您的PHP中(而不是print_r).
Use json_encode in you PHP (instead of print_r).
,您可以使用 $ .getJson :
$.getJSON( 'markers.php', function( data ) {
$.each( data, function( key, val ) {
var position = [val.lat, val.lng];
console.log(position);
});
});
这篇关于如何使用jQuery与来自PHP的信息数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文