2017-12-13 2 views
0

私はphpで新しく、私はこの例で見ることができるように、私はこの配列の結果のようにしたい:親のIDを持つ階層ツリーを作成したい: 私は私はすでにファイル階層のための多次元PHP配列を作成します

Array 
(
[0] => Array 
    (
     [text] => Folder name 1 
     [parent_id] => 
     [id] => 1 
     [children] => Array 
      (
       [0] => Array 
        (
         [text] => Folder name 2 
         [parent_id] => 1 
         [id] => 2 
         [children] => Array 
          (
          ) 

         [data] => stdClass Object 
          (
          ) 

        ) 

      ) 

     [data] => stdClass Object 
      (
      ) 

    ) 
) 

これは私自身の関数の結果

Array 
(
[/] => Array 
    (
     [0] => . 
     [1] => .. 
     [2] => .bash_logout 
     [3] => .bash_profile 
     [4] => .bashrc 
     [5] => .contactemail 
     [6] => .cpanel 
     [7] => .cphorde 
     [8] => .ftpquota 
     [9] => .gemrc 
     [10] => .htpasswds 
     [11] => .lastlogin 
     [12] => .trash 
     [13] => .zshrc 
     [14] => access-logs 
     [15] => etc 
     [16] => logs 
     [17] => mail 
     [18] => perl5 
     [19] => public_ftp 
     [20] => public_html 
     [21] => ssl 
     [22] => tmp 
     [23] => www 
    ) 

[etc] => Array 
    (
     [0] => . 
     [1] => .. 
     [2] => cacheid 
     [3] => ftpquota 
     [4] => hebergementweb-maroc.com 
    ) 

[logs] => Array 
    (
     [0] => . 
     [1] => .. 
     [2] => ftp.hebergementweb-maroc.com-ftp_log-Dec-2017.gz 
     [3] => hebergementweb-maroc.com-Dec-2017.gz 
    ) 

[mail] => Array 
    (
     [0] => . 
     [1] => .. 
     [2] => .Drafts 
     [3] => .Junk 
     [4] => .Sent 
     [5] => .Trash 
     [6] => cur 
     [7] => hebergementweb-maroc.com 
     [8] => mailbox_format.cpanel 
     [9] => new 
     [10] => tmp 
    ) 

[tmp] => Array 
    (
     [0] => . 
     [1] => .. 
     [2] => analog 
     [3] => awstats 
     [4] => cpbandwidth 
     [5] => webalizer 
     [6] => webalizerftp 
     [7] => . 
     [8] => .. 
     [9] => analog 
     [10] => awstats 
     [11] => cpbandwidth 
     [12] => webalizer 
     [13] => webalizerftp 
    ) 

[perl5] => Array 
    (
     [0] => . 
     [1] => .. 
    ) 

[public_ftp] => Array 
    (
     [0] => . 
     [1] => .. 
     [2] => incoming 
    ) 

[public_html] => Array 
    (
     [0] => . 
     [1] => .. 
     [2] => .htaccess 
     [3] => 404.shtml 
     [4] => cgi-bin 
     [5] => default.htm 
     [6] => error_log 
     [7] => favicon.ico 
     [8] => phpinfo.php 
     [9] => robots.txt 
     [10] => test.php 
     [11] => under_construction.html 
    ) 

[ssl] => Array 
    (
     [0] => . 
     [1] => .. 
     [2] => certs 
     [3] => csrs 
     [4] => keys 
     [5] => ssl.db 
     [6] => ssl.db.cache 
    ) 

[www] => Array 
    (
     [0] => . 
     [1] => .. 
     [2] => .htaccess 
     [3] => 404.shtml 
     [4] => cgi-bin 
     [5] => default.htm 
     [6] => error_log 
     [7] => favicon.ico 
     [8] => phpinfo.php 
     [9] => robots.txt 
     [10] => test.php 
     [11] => under_construction.html 
    ) 
) 

で使用することをjstreeする配列を送りたいので、そのような配列を生成したい、これが私のfunctiosnコードです:

あなたはインクルードは、ツリーを構築するために、ローカル配列を使用することができます
function ftpRecursiveFileListing($ftpConnection, $path) { 
static $allFiles = array(); 
$contents = ftp_nlist($ftpConnection, $path); 

foreach($contents as $currentFile) { 
    // assuming its a folder if there's no dot in the name 
    if (strpos($currentFile, '.') === false) { 
     $this->ftpRecursiveFileListing($ftpConnection, $currentFile); 
    } 
    $allFiles[$path][] = $currentFile; 
      //break; 
} 
return $allFiles; 
} 

public function test(){ 
    $ftp_server = "ftp.mywebsite.com"; 
    $ftp_user_name = "[email protected]"; 
    $ftp_user_pass = "mypswd"; 
    // Mise en place d'une connexion basique 
    $conn_id = ftp_connect($ftp_server); 
    // Identification avec un nom d'utilisateur et un mot de passe 
    $login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass); 
    echo "<pre>"; 
    $handler = $this->ftpRecursiveFileListing($conn_id, '/'); 
    print_r($handler); 

} 

多くのおかげ

答えて

0

function ftpRecursiveFileListing($ftpConnection, $path, $parentId) { 
    static $cnt = 0; 
    $files = array(); 
    $contents = ftp_nlist($ftpConnection, $path); 

    foreach($contents as $currentFile) { 
     $currentFileArray = array(); 
     $currentFileArray[text] = $currentFile; 
     $currentFileArray[parentId] = $parentId; 
     $currentFileArray[id] = $cnt++; 
     // assuming its a folder if there's no dot in the name 
     if (strpos($currentFile, '.') === false) 
      $currentFileArray[children] = $this->ftpRecursiveFileListing($ftpConnection, $currentFile, $currentFileArray[id]); 
     $files [] = $currentFileArray; 
    } 
return $files ; 
} 

となり、初めて関数を呼び出します。

$handler = $this->ftpRecursiveFileListing($conn_id, '/', null); 

この基本データのみ - あなたは配列

+0

に複数のキーを追加することができますので、ありがとうございました、神が祝福大丈夫ですあなた:)再びありがとうございます –

+0

あなたは投票の上にあなたの感謝を示すことができ、受け入れられた答えをマークすることができます。あなたは歓迎します –

+0

私は投票する15の評判が必要です:(私はごめんなさい –