PHP多维数组递归问题

时间:2011-09-28 21:26:39

标签: php arrays recursion multidimensional-array

  

可能重复:
  Walk array recursively and print the path of the walk

任何人都可以帮我解决这个问题吗?

<?php
function buildMenuWalk(&$array, &$depth, $currentDepth = 1)
    {
        # start new level html
        $html = '';

        # walk till the depth defined on the config
        if($currentDepth > $depth)
        {
            return null;
        }

        # loop through all items in this level
        foreach($array as $key => &$value)
        {       
            # if not in area map continue
            if(!is_numeric($key))
            {
                continue;
            }

            # if no <li> has been created yet, open the <ul>
            $html .= empty( $html ) ? '<ul class="dropdown">' : ''; 

            #extract the label from this level's array, designated by $labelKey
            $label = isset( $value['areaname'] ) ? $value['areaname'] : '';

            # open an <li>
            $html .= '<li>';

            # generate url
            $url = '';
            if($currentDepth == $depth)
            {
                $url = ' href="'.

                $url .= '"';
            }

            # construct content inside the <li>
            $html .= '<a' . $url .'>' . $label . '</a>';


            # run the function again to grab children levels
            if(is_array($value))
            {
                $html .= buildMenuWalk($value, $depth, $currentDepth + 1);
            }

            # close <li>
            $html .= '</li>';
        }

        # close <ul> if was generated content on this level
        $html .= !empty( $html ) ? '</ul>' : '';

        return $html;
    }
$depth = 2;

$config['content']['map'][1]['areaname'] = 'area_1';
$config['content']['map'][1][1]['areaname'] = 'block_1';
$config['content']['map'][2]['areaname'] = 'area_2';
$config['content']['map'][2][1]['areaname'] = 'block_1';
$config['content']['map'][2][2]['areaname'] = 'bloack_2';

echo buildMenuWalk($config['content']['map'], $depth);
?>

如果你检查上面的代码,我用来递归地显示一个菜单......

如果脚本识别出菜单到达深度,则会在标签内显示href =“”。在这个href里面我想在递归上添加所有父区域。

例如:

$config['content']['map'][2]['areaname'] = 'area_2';
$config['content']['map'][2][1]['areaname'] = 'block_1';
$config['content']['map'][2][2]['areaname'] = 'bloack_2';

当脚本到达bloack_2时,我需要显示:

<a href="area_2=2&block_2=2">

由于它是一个多维数组并且可以增长到4-5维,因此输出href应该计算所有这些级别。例如,area_2 = 1&amp; block_10 = 5&amp; sub_area_1 = 5&amp; section_7 = 8 ...

我可能需要一些数组来存储递归过程中的所有href路径,但我不知道该怎么做。

谢谢,

PS:该脚本将用于构建下拉菜单。父级别不需要链接,因此打印标签将显示子菜单。最后一个孩子将被链接,但需要包含所有父参数,以便可以过滤结果。

链接到运行和返回值的代码: http://codepad.org/iyrcdfQP

3 个答案:

答案 0 :(得分:0)

由于此函数以递归方式调用自身,因此您必须引用全局变量来跟踪前一个树。所以这里的代码就是我认为您希望它做的事情。你们所有人都必须改变#generate url部分。

# generate url
$url = '';
if($currentDepth == $depth) {
  $url = " href=area=" . $GLOBALS['area'] . "&block=$key";
} else {
  $GLOBALS['area'] = $key;
}

答案 1 :(得分:0)

这是对同一片代码的修改,使得href =更像你所描述的那样,虽然它对我来说没有意义。

    # generate url
    $url = '';
    if($currentDepth == $depth) {
      $url = " href=".$GLOBALS['area_name']."=".$GLOBALS['area']."&$label=$key";
    }
    else {
      $GLOBALS['area'] = $key;
      $GLOBALS['area_name'] = $label;
    }

答案 2 :(得分:0)

我不明白为什么人们会因为网页中的菜单而在2维的世界中使用recursoin和多个维度而烦恼!

我只会选择2个维度,第一个维度是保留所有元素(没有任何条件),第二个维度是保留关于元素的信息:

enter image description here

为自己检查一个示例,设置要使该函数显示的元素以及之前级别的所有祖先和根元素(如加利福尼亚州)。

<?php
function buildMenuWalk($element,$menuElements)
{
    for($i=0;$i<=$element;$i++)
    {
        //for a new level go to the next line
        if(!isset($menuElements[$i]['ancestors']))
        {
            echo '<br>______________________________________</br>';
            echo '<strong>',$menuElements[$i]['label'],'</strong> | ';
        }
        //if the element is reached display it along with ancestors
        if($i==$element)
        {
            //echo all the ancestores
            foreach($menuElements[$element]['ancestors'] as $value)
            { 
                echo $menuElements[$value]['label'],' | ';
            }
            //display the element itself
                echo '<font color=red>',$menuElements[$element]['label'],' | </font>';          
        } 
    }
}

//California
$menuElements[0]=Array('url'=>'http://www.California.com','label'=>'California');
$menuElements[1]=Array('url'=>'http://www.San Diego.com','label'=>'San Diego','ancestors'=>Array(0));
$menuElements[2]=Array('url'=>'http://www.San Jose.com','label'=>'San Jose','ancestors'=>Array(0,1));
$menuElements[3]=Array('url'=>'http://www.San Francisco.com','label'=>'San Francisco','ancestors'=>Array(0,1,2));
$menuElements[4]=Array('url'=>'http://www.Fresno.com','label'=>'San Francisco','ancestors'=>Array(0,1,2,3));
$menuElements[5]=Array('url'=>'http://www.Sacramento.com','label'=>'Sacramento','ancestors'=>Array(0,1,2,3,4));

//Wyoming
$menuElements[6]=Array('url'=>'http://www.Wyoming.com','label'=>'Wyoming');
$menuElements[7]=Array('url'=>'http://www.Cheyenne.com','label'=>'Cheyenne','ancestors'=>Array(6));
$menuElements[8]=Array('url'=>'http://www.Casper.com','label'=>'Casper','ancestors'=>Array(6,7));
$menuElements[9]=Array('url'=>'http://www.Laramie.com','label'=>'Laramie','ancestors'=>Array(6,7,8));
$menuElements[10]=Array('url'=>'http://www.Gillette.com','label'=>'Gillette','ancestors'=>Array(6,7,8,9));
$menuElements[11]=Array('url'=>'http://www.Rock Springs.com','label'=>'Rock Springs','ancestors'=>Array(6,7,8,9,10));

 echo '<pre>'; 
 buildMenuWalk(9,$menuElements);

?>