[英]parse nested xml with php
Hello i am trying to parse nested xml but i am using multiple for loops to extact but i dont find it very proper way. 您好,我正在尝试解析嵌套的xml,但是我正在使用多个for循环来执行操作,但是我找不到非常合适的方法。
I want to parse data based on unique id and separte them but cant figure any easy way to that 我想根据唯一的ID解析数据并将其分离,但无法找到任何简单的方法
here is my xml 这是我的xml
<propertyList date="2014-06-02-17:30:33" username="adsss" password="v147C0z5CBlw">
<business modTime="2014-06-02-12:22:32" status="current">
<agentID>TEST</agentID>
<uniqueID>1420648</uniqueID>
<listingAgent id="1">
<name>jonny</name>
<telephone type="BH"></telephone>
<telephone type="mobile"></telephone>
<email>aT@cs.com.au</email>
</listingAgent>
<listingAgent id="2"></listingAgent>
</business>
<business modTime="2014-05-16-15:11:41" status="current">
<agentID>TEST</agentID>
<uniqueID>1435201</uniqueID>
<listingAgent id="1">
<name>Mike Smith</name>
<telephone type="BH"></telephone>
<telephone type="mobile"></telephone>
<email>acebrokers@bs.com.au</email>
</listingAgent><listingAgent id="2"></listingAgent>
</business>
</propertyList>
Here is my code 这是我的代码
<?php
$xml=simplexml_load_file("myxmldata.xml");
echo "<pre>";
for($i=0;$i<count($xml);$i++):
echo "agentID=>".$xml->business[$i]->agentID;
echo "<br>";
echo "uniqueID=>".$xml->business[$i]->uniqueID;
echo "<br>";
endfor;
for($i=0;$i<count($xml->business->listingAgent);$i++):
echo "agentName=>".$xml->business->listingAgent[$i]->name;
endfor;
for($i=0;$i<count($xml->business->listingAgent->telephone);$i++):
echo "telephonetype=>".$xml->business->listingAgent->telephone[$i]->type;
endfor;
?>
Try this: 尝试这个:
$xml = simplexml_load_file("myxmldata.xml");
$data = array();
foreach($xml->business as $business) {
$business = (array) $business;
if (!array_key_exists($business['uniqueID'], $data)) {
$listingAgent = (array) $business['listingAgent'];
$data[$business['uniqueID']] = array(
'agentID' => $business['agentID'],
'uniqueID' => $business['uniqueID'],
'name' => (string)$listingAgent[0]->name,
'telephone' => (string) $listingAgent[0]->telephone[0],
'mobile' => (string) $listingAgent[0]->telephone[1],
'email' => (string) $listingAgent[0]->email,
);
}
}
var_dump($data);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.