有没有更好的方法来循环这个 XML 列表
这是循环
function List($input)
{ //I'm sure there's a better way of doing this...
$list = simplexml_load_string($input);
foreach($list->nation as $nation){
foreach($nation->region as $region){
$temp['url']= (string)$region['url'];
$temp['name'] = (string)$region['name'];
$temp['nation'] = (string)$nation['name'];
$out['regions'][] = $temp;
}
$temp['url'] = (string)$nation['url'];
$temp['name'] = (string)$nation['name'];
unset($temp['nation']);
$out['nations'][] = $temp;
}
return $out;
}
示例数据:
<?xml version="1.0"?>
<regions>
<nation url="canada" name="Canada" >
<region url="abbotsford" name="Abbotsford" />
<region url="barrie" name="Barrie" />
<region url="brantford" name="Brantford" />
<region url="calgary" name="Calgary" />
<region url="charlottetown" name="Charlottetown" />
</nation>
<nation url="ireland" name="Ireland">
<region url="cork" name="Cork"/>
<region url="dublin" name="Dublin"/>
<region url="galway" name="Galway"/>
<region url="limerick" name="Limerick"/>
<region url="waterford" name="Waterford"/>
</nation>
感谢您的帮助
Here's the loop
function List($input)
{ //I'm sure there's a better way of doing this...
$list = simplexml_load_string($input);
foreach($list->nation as $nation){
foreach($nation->region as $region){
$temp['url']= (string)$region['url'];
$temp['name'] = (string)$region['name'];
$temp['nation'] = (string)$nation['name'];
$out['regions'][] = $temp;
}
$temp['url'] = (string)$nation['url'];
$temp['name'] = (string)$nation['name'];
unset($temp['nation']);
$out['nations'][] = $temp;
}
return $out;
}
Sample Data:
<?xml version="1.0"?>
<regions>
<nation url="canada" name="Canada" >
<region url="abbotsford" name="Abbotsford" />
<region url="barrie" name="Barrie" />
<region url="brantford" name="Brantford" />
<region url="calgary" name="Calgary" />
<region url="charlottetown" name="Charlottetown" />
</nation>
<nation url="ireland" name="Ireland">
<region url="cork" name="Cork"/>
<region url="dublin" name="Dublin"/>
<region url="galway" name="Galway"/>
<region url="limerick" name="Limerick"/>
<region url="waterford" name="Waterford"/>
</nation>
Thanks for any help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)