首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将多维数组显示为符号点

问将多维数组显示为符号点
EN

Stack Overflow用户
提问于 2013-05-25 11:59:28
回答 1查看 667关注 0票数 1

我需要帮助设计一个递归函数:

  1. 递归显示多维数组。
  2. 必须使用要点,例如<ul>和<li>
  3. 文本必须链接到文档。例如,文本“1.hi”将被重定向,保持其目录结构(Array结构)。但文本必须递归显示。

将作为输入的数组将是目录层次结构的多维数组。输入:

代码语言:javascript
复制
Array
(
    [pocketmine] => Array
        (
            [tutorial] => Array
                (
                    [0] => 1. Introduction.markdown
                    [1] => part3-setting-up-your-workspace.php
                    [2] => part1-introduction.php~
                    [3] => part2-planning.php~
                    [4] => part2-planning.php
                    [5] => part5-pocketmine-says-hello-world.php
                    [6] => part4-inter-lingual-programming-basics.php
                )

            [documentation] => Array
                (
                    [0] => List of Events Alphabetical.markdown
                )

        )

)

第一个输出将是

  • 麻黄碱
    • 教程
      • 1.Introduction

​

​

但是"1.介绍“必须链接到"whateverURL/pocketmine/tutorial/1.Introduction”

我需要将这个显示为(例如)

代码语言:javascript
复制
<ul>
<li><a href=pocketmine/tutorial/1.-Introduction.markdown>1. Introduction</li>
</ul>

谢谢你提前帮忙。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-05-25 13:24:47

使用RecursiveArrayIterator处理数据,使用RecursiveIteratorIterator处理模板,您可以以所需的方式显示数据。下面是一个快速实现

代码语言:javascript
复制
<?php

$data = array(
    'pocketmine' => array
    (
        'tutorial' => array
        (
            '1.Introduction.markdown',
            'part3-setting-up-your-workspace.php',
            'part1-introduction.php~',
            'part2-planning.php~',
            'part2-planning.php',
            'part5-pocketmine-says-hello-world.php',
            'part4-inter-lingual-programming-basics.php',
        ),
        'documentation' => array
        (
            'List of Events Alphabetical.markdown'
        ),

    )   
);

class ListIterator extends RecursiveIteratorIterator
{
    protected $url_path;
    protected $top_path;

    public function beginChildren()
    {
        echo '<ul>';
    }

    public function endChildren()
    {
        //Reset url path when changing children.
        $this->url_path = "";

        echo '</ul></li>';
    }

    public function current()
    {
        // if current item has children then print only the key.
        if ($this->callHasChildren()) {
            // Store top path for urls
            if ($this->getDepth() == 0) {
                $this->top_path = $this->key();
            } else {
                // create the url path from array keys.
                $this->url_path .= "/" . $this->key();
            }
            return $this->key();
        } else {
            // Get only the title without dot extension.
            $dot = strrpos(parent::current(), '.');
            if ($dot !== false) {
                $page = substr(parent::current(), 0, $dot);
            } else {
                $page = parent::current();
            }
            // compose final url path
            $path = $this->top_path . $this->url_path . "/" . parent::current();

            return '<a href="'.$path.'">'. $page . '</a>';
        }
    }

    public function beginIteration()
    {
        echo '<ul>';
    }

    public function endIteration()
    {
        echo '</ul>';
    }
}

$iterator = new RecursiveArrayIterator($data);

$template = new ListIterator($iterator, RecursiveIteratorIterator::SELF_FIRST);

foreach ($template as $item) {
    echo '<li>' . $item;
    echo $template->callHasChildren() == false ? '</li>' : null;
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16749325

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档