2016-03-14 83 views
1

檢索數組中的文件結構我想從dynatree的php中的特定路徑中檢索這種類型的數組? 如何以檢索文件和文件夾這樣的陣列...如何在php中使用函數

children: [ 
     {title: "Item 1", key: "node1"}, 
     {title: "Folder 2", isFolder: true, key: "node2", 
      children: [ 
       {title: "Sub-item 2.1", key: "node2.1"}, 
       {title: "Sub-item 2.2", key: "node2.2"} 
      ] 
     }, 
     {title: "Item 3", key: "node3"} 
     ] 

我嘗試這個功能...

<?php 

     function getDirectory($path = '.', $level = 0) { 
      $ignore = array('cgi-bin', '.', '..'); 
      $dh = @opendir($path); 

      while (false !== ($file = readdir($dh))) { 
       if (!in_array($file, $ignore)) { 
       if (is_dir("$path/$file")) { 
        echo '<li id="key1" class="folder expanded">' . $file; 
        echo '<ul class="folder-content">'; 
        getDirectory("$path/$file", ($level + 1)); 
        echo '</ul>'; 
        echo '</li>'; 
        } 
       } 
      } 
     closedir($dh); 
    } 
    ?> 
+0

你試過了嗎?請告訴我們你的代碼。 – aslawin

+0

看到我加我嘗試過的功能... @aslawin – Brothers

回答

1

你可以試試這個功能:

function getDirectory($path = '.', $level = 1) 
{ 
    $result = array(); 
    $ignore = array('nbproject', 'src', '.', '..'); 
    $dh = @opendir($path); 

    $i = 0; 
    while($file = readdir($dh)) 
    { 
     if(!in_array($file, $ignore)) 
     { 
      if(is_dir($path.'/'.$file)) 
      { 
       $level++; 
       $singleResult = array('title'=>$file, 'isFolder'=>true, 'children'=>$this->getDirectory($path.'/'.$file, $level), 'key'=>'node'.$level); 
       $result[] = $singleResult; 
      } 
      else 
      { 
       $result[] = array('title'=>$file, 'key'=>'node'.$level.'.'.$i); 
      } 
     } 

     $i++; 
    } 

    closedir($dh); 

    return $result; 
} 

我用掃描這個功能我的目錄之一,它輸出:

array 
(
    0 => array 
    (
     'title' => 'CHANGELOG.txt' 
     'key' => 'node1.2' 
    ) 
    1 => array 
    (
     'title' => 'composer.json' 
     'key' => 'node1.3' 
    ) 
    2 => array 
    (
     'title' => 'docs' 
     'isFolder' => true 
     'children' => array 
     (
      0 => array 
      (
       'title' => 'COMPUTER-FUNCTIONS.md' 
       'key' => 'node2.2' 
      ) 
      1 => array 
      (
       'title' => 'CONFIGURATION.md' 
       'key' => 'node2.3' 
      ) 
      2 => array 
      (
       'title' => 'CONTACT-FUNCTIONS.md' 
       'key' => 'node2.4' 
      ) 
      3 => array 
      (
       'title' => 'EXCHANGE-FUNCTIONS.md' 
       'key' => 'node2.5' 
      ) 
      4 => array 
      (
       'title' => 'FOLDER-FUNCTIONS.md' 
       'key' => 'node2.6' 
      ) 
      5 => array 
      (
       'title' => 'GETTING-STARTED.md' 
       'key' => 'node2.7' 
      ) 
      6 => array 
      (
       'title' => 'GROUP-FUNCTIONS.md' 
       'key' => 'node2.8' 
      ) 
      7 => array 
      (
       'title' => 'SEARCH-FUNCTIONS.md' 
       'key' => 'node2.9' 
      ) 
      8 => array 
      (
       'title' => 'UPGRADING.md' 
       'key' => 'node2.10' 
      ) 
      9 => array 
      (
       'title' => 'USER-FUNCTIONS.md' 
       'key' => 'node2.11' 
      ) 
     ) 
     'key' => 'node2' 
    ) 
    3 => array 
    (
     'title' => 'examples' 
     'isFolder' => true 
     'children' => array 
     (
      0 => array 
      (
       'title' => 'examples.php' 
       'key' => 'node3.2' 
      ) 
      1 => array 
      (
       'title' => 'groupCollection.php' 
       'key' => 'node3.3' 
      ) 
      2 => array 
      (
       'title' => 'index.php' 
       'key' => 'node3.4' 
      ) 
      3 => array 
      (
       'title' => 'userCollection.php' 
       'key' => 'node3.5' 
      ) 
      4 => array 
      (
       'title' => 'view.html.php' 
       'key' => 'node3.6' 
      ) 
     ) 
     'key' => 'node3' 
    ) 
    4 => array 
    (
     'title' => 'LICENSE.txt' 
     'key' => 'node3.6' 
    ) 
    5 => array 
    (
     'title' => 'phpunit.xml' 
     'key' => 'node3.8' 
    ) 
    6 => array 
    (
     'title' => 'README.md' 
     'key' => 'node3.9' 
    ) 
    7 => array 
    (
     'title' => 'tests' 
     'isFolder' => true 
     'children' => array 
     (
      0 => array 
      (
       'title' => 'AdldapBaseTest.php' 
       'key' => 'node4.2' 
      ) 
      1 => array 
      (
       'title' => 'AdldapConstructTest.php' 
       'key' => 'node4.3' 
      ) 
      2 => array 
      (
       'title' => 'AdldapLiveTest.php' 
       'key' => 'node4.4' 
      ) 
      3 => array 
      (
       'title' => 'AdldapMethodTest.php' 
       'key' => 'node4.5' 
      ) 
      4 => array 
      (
       'title' => 'AdldapObjectTest.php' 
       'key' => 'node4.6' 
      ) 
      5 => array 
      (
       'title' => 'Classes' 
       'isFolder' => true 
       'children' => array 
       (
        0 => array 
        (
         'title' => 'AdldapSearchTest.php' 
         'key' => 'node5.2' 
        ) 
        1 => array 
        (
         'title' => 'AdldapUsersTest.php' 
         'key' => 'node5.3' 
        ) 
        2 => array 
        (
         'title' => 'AdldapUtilityTest.php' 
         'key' => 'node5.4' 
        ) 
       ) 
       'key' => 'node5' 
      ) 
      6 => array 
      (
       'title' => 'ConnectionTest.php' 
       'key' => 'node5.8' 
      ) 
      7 => array 
      (
       'title' => 'FunctionalTestCase.php' 
       'key' => 'node5.9' 
      ) 
      8 => array 
      (
       'title' => 'Objects' 
       'isFolder' => true 
       'children' => array 
       (
        0 => array 
        (
         'title' => 'AccountControlTest.php' 
         'key' => 'node6.2' 
        ) 
        1 => array 
        (
         'title' => 'ContactTest.php' 
         'key' => 'node6.3' 
        ) 
        2 => array 
        (
         'title' => 'LdapEntryTest.php' 
         'key' => 'node6.4' 
        ) 
        3 => array 
        (
         'title' => 'MailboxTest.php' 
         'key' => 'node6.5' 
        ) 
        4 => array 
        (
         'title' => 'PagintorTest.php' 
         'key' => 'node6.6' 
        ) 
        5 => array 
        (
         'title' => 'UserTest.php' 
         'key' => 'node6.7' 
        ) 
       ) 
       'key' => 'node6' 
      ) 
     ) 
     'key' => 'node4' 
    ) 
) 

當然,你需要自己實現將數組轉換爲json,只需要掃描目錄並給它適當的參數。

+0

好吧謝謝uhhh – Brothers

+0

現在如何將這個值發送到另一個php頁面@aslawin – Brothers

+0

@KevinPatel首先嚐試,然後如果你將無法發佈另一個問題。 –