我是php的新手,我想创建一个带有父ID的层次结构树,如示例中所示,我想像这个数组结果一样: 我想生成一个这样的数组,因为我想将数组发送到我已经使用的jstree
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
)
)
这是我的功能代码:
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 = "user@mywebsite.com";
$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 :(得分: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);
这只是基本数据 - 您可以向数组中添加更多键