I'm building something so I can parse latitudes and longitudes from an XML file. The problem is that I have a users with multiple lat and lng (used for markers on google maps) coordinates and only the first coordinate is saved in the array. I would like to have every coordinate in the array. It looks like that the foreach function isn't working properly
我正在构建一些东西,这样我就可以从XML文件中解析纬度和经度。问题是,我的用户具有多个lat和lng(用于谷歌地图上的标记)坐标,并且只有第一个坐标保存在数组中。我想要数组中的每个坐标。看起来foreach函数没有正常工作
Here is the ajax call to the php file where I parse the xml file. And also test if the parsed data is working with json.
这里是对php文件的ajax调用,我在这里解析xml文件。还要测试解析后的数据是否使用json。
<html>
<head>
<script type="text/javascript" src="jquery-2.1.0.js"></script>
<script>
$(function()
{
$.ajax(
{
type:"GET",
url:"leesstudent.php",
success:callback
}
);
});
function callback(data,status)
{
alert(data);
var jsArr = JSON.parse(data);
var coordinates = new Array();
alert(jsArr[0]);
for(var i=0;i<jsArr.length;i++)
{
$("#message").append("<p>" + jsArr[i].latitude + " " + jsArr[i].longitude + "</p>");
}
}
</script>
</head>
<body>
<div id="message">
Message..
</div>
</body>
The php file where I parse the xml, from my opinion the foreach doesn't work
根据我的看法,我解析xml的php文件foreach不起作用
$xml = simplexml_load_file("Database.xml");
$coordinaten = array();
$teller = 0;
foreach($xml->user as $item)
{
$coordinaten[$teller]["latitude"] = (string)$item -> latitude;
$coordinaten[$teller]["longitude"] = (string)$item -> longitude;
$teller++;
}
print json_encode($coordinaten);
the xml code
xml代码
<root>
<user>
<username>$2y$11$6SxUsvoDGlwm3Ji6BcnzCu/QyUWNy09Ny/.M9rXIpvImgJ1igJppy</username>
<password>$2y$11$6SxUsvoDGlwm3Ji6BcnzCu7hIhAyNKtdlui1.oMdK4gQJnkZrL/Ky</password>
<latitude>50.74688365485319</latitude><longitude>5.0701904296875</longitude>
<latitude>51.09662294502995</latitude><longitude>4.9713134765625</longitude>
</user>
</root>
I only get the first latitude and longitude data, I would like have both (and in the future even more).
我只获得了第一个纬度和经度数据,我希望同时拥有这两个数据(将来甚至更多)。
1 个解决方案
#1
1
Your foreach loop isn't correct. He will loop trough the users but never loop trough your coordinats!
你的foreach循环不正确。他会对用户进行循环,但从不对协调器进行循环!
foreach($xml->user as $item){
$teller = 0;
foreach($item -> latitude as $test )
{
$coordinaten[$teller]["latitude"] = (string)$test;
$teller++;
}
$teller = 0;
foreach($item -> longitude as $test2)
{
$coordinaten[$teller]["longitude"] = (string)$test2;
$teller++;
}
}
#1
1
Your foreach loop isn't correct. He will loop trough the users but never loop trough your coordinats!
你的foreach循环不正确。他会对用户进行循环,但从不对协调器进行循环!
foreach($xml->user as $item){
$teller = 0;
foreach($item -> latitude as $test )
{
$coordinaten[$teller]["latitude"] = (string)$test;
$teller++;
}
$teller = 0;
foreach($item -> longitude as $test2)
{
$coordinaten[$teller]["longitude"] = (string)$test2;
$teller++;
}
}