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条)

报告相同问题?

悬赏问题

  • ¥50 求解vmware的网络模式问题 别拿AI回答
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥30 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?