doudou5023 2012-03-14 21:48
浏览 43
已采纳

PHP递归数组循环和格式化

I have some PHP code I've been working on for a good few days now. I'm trying to generate a formatted list of rules from a flat array. I got help here before on how to turn the flat array into a tree array, but I'm having difficulty writing a recursive function that can go through it and successfully break it down at points at such depths where I'd like the rules to be members of an unordered list from the markup that gets printed.

<?php 
$data = array(
    '0'        => 'Introduction',
    '4'        => 'General',
    '4.1'      => 'Chat',
    '4.1.1'    => 'Do',
    '4.1.1.9'  => 'This',
    '4.1.1.10' => 'That',
    '4.1.1.11' => 'Other',
);

$struct = array(
    'children' => array()
);

foreach ($data as $ruleID => $content)
{
    $parent =& $struct;
    foreach (explode('.', $ruleID) as $val)
    {
        if (!isset($parent['children'][$val]))
        {
        $parent['children'][$val] = array(
                'content' => '',
                'children' => array()
            );
        }
        $parent =& $parent['children'][$val];
    }
    $parent['content'] = $content;
}


$out = '';
$rules = array_pop($struct);
format_rule($rules);
var_dump($rules);
echo $out;

function format_rule($arr, $depth=0)
{
    global $out;
    echo "depth: $depth
";
    foreach($arr as $key => $val)
    {
        switch($depth)
        {
            case 0:
                $out .= '<h1>'.$val['content']."</h1><br />
";
                break;
            case 1:
                $out .= '<h2>'.$val['content']."</h2><br />
";
                break;
            case 2:
                $out .= '<h3>'.$val['content']."</h3><br />
";
                break;
            default:
                $out .= '<li>'.$val['content']."</li>
";
                break;
        }
        if(isset($val['children']) && count($val['children']) > 0)
        {
            if($depth > 2)
            {
                $out .= '<ul>';
                format_rule($val['children'], ++$depth);
                $out .= '</ul>';
            }
            else
            {
                format_rule($val['children'], ++$depth);
            }
        }
    }
}

The output at the moment is:

<h1>Introduction</h1><br />
<h1>General</h1><br />
<h2>Chat</h2><br />
<h3>Do</h3><br />
<li>This</li><br />
<li>That</li><br />
<li>Other</li><br />

Which is great, except from my code I'm pretty sure the section under 'Do' should have a <ul> around it.

  • 写回答

2条回答 默认 最新

  • dongqiang4819 2012-03-14 21:59
    关注

    change your code to :

    if($depth >= 2)
    

    note: remember the count starts at 0, not 1.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥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 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看