有没有更好的方法来循环这个 XML 列表

发布于 2024-11-18 07:57:53 字数 1414 浏览 1 评论 0原文

这是循环

 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

女中豪杰 2024-11-25 07:57:53
function List($input) {
    $list = simplexml_load_string($input);

    // guarantee fixed output format
    $out = array('regions' => array(), 'nations' => array());

    foreach ($list->nation as $nation){
        foreach ($nation->region as $region){
            $out['regions'][] = array(
                'url'    => $region['url'],
                'name'   => $region['name'],
                'nation' => $nation['name']
            );
        }

        $out['nations'][] = array(
            'url'  => $nation['url'],
            'name' => $nation['name']
        );
    }

    return $out;        
}
function List($input) {
    $list = simplexml_load_string($input);

    // guarantee fixed output format
    $out = array('regions' => array(), 'nations' => array());

    foreach ($list->nation as $nation){
        foreach ($nation->region as $region){
            $out['regions'][] = array(
                'url'    => $region['url'],
                'name'   => $region['name'],
                'nation' => $nation['name']
            );
        }

        $out['nations'][] = array(
            'url'  => $nation['url'],
            'name' => $nation['name']
        );
    }

    return $out;        
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文