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 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大