1

次のことを行う再帰関数の設計について助けが必要です。

  1. 多次元配列を再帰的に表示します
  2. <ul>やなどの箇条書きを使用する必要があります<li>
  3. テキストはドキュメントにリンクしている必要があります。たとえば、テキスト「1.こんにちは」は、ディレクトリ構造 (配列構造) を維持したままリダイレクトされます。ただし、テキストは再帰的に表示する必要があります。

入力となる配列は、ディレクトリ階層の多次元配列になります。入力:

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.はじめに

ただし、「1.はじめに」は「whateverURL/pocketmine/tutorial/1.はじめに」にリンクする必要があります。

これを次のように表示する必要があります(たとえば)

<ul>
<li><a href=pocketmine/tutorial/1.-Introduction.markdown>1. Introduction</li>
</ul>

事前にご協力いただきありがとうございます。

4

1 に答える 1

1

データの処理にRecursiveArrayIteratorを使用し、テンプレート化にRecursiveIteratorIteratorを使用すると、データを希望どおりに表示できます。これが高速な実装です

<?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;
}
于 2013-05-25T13:24:47.600 に答える