2013-05-25 81 views
0

我需要幫助設計一個遞歸函數,它:顯示多維數組要點

  1. 顯示多維數組遞歸例如
  2. 必須使用要點<ul><li>
  3. 文本必須鏈接到文檔。例如文本「1. Hi」將被重定向保持其目錄結構(數組結構)。但文本必須遞歸顯示。

將作爲輸入的數組將是目錄層次結構的多維數組。 輸入:

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 
       ) 

     ) 

) 

爲第一個的輸出將是

  • Pocketmine
    • 教程
      • 1.簡介

但是「1。簡介」必須鏈接到 「whateverURL/pocketmine /教程/一,簡介」

我需要這個顯示爲(例如)

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

感謝您的幫助提前。

+1

你嘗試過什麼嗎?如果是這樣的,發佈你有的任何代碼,並描述它是如何失敗的。 – vascowhite

回答

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; 
} 
+0

甜!非常感謝! –