dongsi1944 2013-02-16 07:45
浏览 32
已采纳

如何从PHP中的递归函数计算xml的元素?

Here's what i've tried but the numbers is wrong and I don't know why

enter image description here

It should be 1, 2, 3, 4, 5, and etc..

Here is my PHP code:

function GetNavigations(SimpleXMLElement $element, $level = 0, $mrg = 0)
{   
    $value = trim((string) $element); 
    $children = $element->children(); 
    $attributes = $element->attributes();
    //echo '<ul>';  
    if(count($children) == 0 && !empty($value))
    {   
    if($element->getName() == 'GroupName')
        {
            if($attributes['ParentId'] != '')
            {
                //$mrg = $level/2 * 10;
                echo '<li>'.$mrg.'<a class="btngroup" href="load.php?active=menu&group_name_id='.$attributes['GroupNameId'].'">'.$element.'</a></li>';
            }   
        }
    }

    if(count($children))
    {
        foreach($children as $child)
        {
            GetNavigations($child, $level+1, $mrg+1);
        } 
    }
    //echo '</ul>';
}
  • 写回答

1条回答 默认 最新

  • douba3943 2013-02-16 21:47
    关注

    GetNavigations($child, $level+1, $mrg+1) will pass the same value of $mrg to all children of a node, however many children it has, because $mrg is not being changed anywhere else inside that loop. Instead of $mrg+1, you could pass ++$mrg - or more readably, add $mrg++; as the line before and just pass $mrg.

    However, you will still have the problem that the function only knows how many direct children have been displayed, not how many descendants - if you call GetNavigations with an $mrg value of 2, and it displays 20 nested items, your next value of $mrg will be 3, not 23! Although they all have the same name, each time you run the function, you have a new $mrg variable.

    To get around that, you can either:

    • Pass $mrg in by reference (by changing the function declaration to be function GetNavigations(SimpleXMLElement $element, $level = 0, &$mrg) with the added &, so that all copies of the function can write to the same variable.
    • Pass the new value of $mrg out as the return value of the function.

    I would probably prefer the second approach, as it's a bit clearer to anyone reading the code what's going on:

    function GetNavigations(SimpleXMLElement $element, $level = 0, $mrg = 0)
    {   
        /* [snip] */
    
        if($element->getName() == 'GroupName')
        {
                // Increment counter, because we're displaying something
                $mrg++;
    
                /* [snip] */
        }
    
        /* [snip] */
    
        if(count($children))
        {
            foreach($children as $child)
            {
                // Recurse, and get incremented value of counter
                $mrg = GetNavigations($child, $level+1, $mrg);
            } 
        }
    
        /* [snip] */
    
        // Let caller know where the counter has got to
        return $mrg;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?