1

I need help designing a recursive function that does:

  1. Displays a multidimensional array recursively
  2. Must use bullet points e.g. <ul> and <li>
  3. Text must link to a document. For example text "1. Hi" will be redirected keeping its directory structure(Array Structure). But the text must be displayed recursively.

The array that will be the input will be a multidimensional array of directory hierarchy. Input:

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
                )

        )

)

The output for the first one will be

  • Pocketmine
    • Tutorial
      • 1.Introduction

But "1. Introduction" must link to "whateverURL/pocketmine/tutorial/1.Introduction"

I need this to be displayed as(for example)

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

Thanks for your help in advance.

SOFe
  • 7,867
  • 4
  • 33
  • 61
Michael Yoo
  • 489
  • 1
  • 6
  • 14

1 Answers1

1

Using RecursiveArrayIterator for handling data and RecursiveIteratorIterator for templating you can display your data the way you want. Here is a fast implementation

<?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;
}
akDeveloper
  • 1,048
  • 11
  • 11