首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从FTP数组返回的PHP排序列表

从FTP数组返回的PHP排序列表
EN

Stack Overflow用户
提问于 2019-06-08 10:06:10
回答 1查看 82关注 0票数 0

我从ftp服务器以数组形式返回了文件:

代码语言:javascript
复制
Array
(
    [0] => FOLDER./Images.Vacation.2018
    [1] => ./Images.Vacation.2018/Racoon001.jpg
    [2] => ./Images.Vacation.2018/Racoon002.txt
    [3] => ./Images.Vacation.2018/the.racoon.swimming.mp4
    [4] => ./Images.Vacation.2018/the.racoon.swimming.slv
    [5] => FOLDER./WestImages
    [6] => ./WestImages/Ramco.txt
    [7] => ./WestImages/Ramco.jpg
    [8] => FOLDER./WestImages/Subimages
    [9] => ./WestImages/Subimages/Image001.jpg
    [10] => ./WestImages/Subimages/Image002.jpg
    [11] => ./WestImages/West.Ramco.Swimming.mp4
)

如您所见,SubImages在WestImages之前返回,这是wrong...so,我无法使用和

  • 标签.我需要数组来返回这个: 数组( ./Images.Vacation.2018/the.racoon.swimming.mp4文件夹./Images.Vacation.2018 1 => ./Images.Vacation.2018/Racoon001.jpg 2 => ./Images.Vacation.2018/Racoon002.txt 3 => => 4 => 5 =>文件夹./WestImage 6 => ./WestImage/Ramco.txt 7 => ./WestImage/Ramco.jpg 8 => ./WestImage/West.Ramco.Swimming.mp4 9 =>文件夹./韦斯特图像/子图像10 => ./ => /子图像/Image001.jpg 11 =>./WestImage/Subimages/Image002.jpg) 这是我从服务器读取文件的ftp代码: /* contents ftp函数*/函数ftp_list_files_recursive($ftp_stream,$path){ $lines = ftp_rawlist($ftp_stream,$path);$result = array();foreach ($lines as $line){ $tokens =B爆发(“",$line);$name = $tokenscount($tokens) - 1;$type = $tokens;$type=。"/“。$name;if ($type == 'd'){ $result[] =‘result[]’. $filepath;$result = array_merge($result,ftp_list_files_recursive($ftp_stream,$filepath));} /*内容- ftp */ $contents = ftp_list_files_recursive($conn_id,".");echo‘;print_r($contents);echo’; 我尝试使用sort($contents);但是它返回错误的sorting...how来重写函数以返回正确的排序,就像上面给出的那样? 谢谢。
EN

回答 1

Stack Overflow用户

发布于 2019-06-08 11:12:04

这不是一个很好的解决方案,但是您可以使用preg_grep获取文件夹名,然后循环它们并对文件进行grep。

代码语言:javascript
复制
$folders = preg_grep("/^FOLDER.*/", $arr);

foreach($folders as $folder){
    $result[] = $folder;
    $result = array_merge($result, preg_grep("/^\." . str_replace(["FOLDER.", "/"], ["","\/"], $folder) . "\/[A-Za-z0-9\.]+$/", $arr));
}

var_dump($result);

https://3v4l.org/BthA0

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56505364

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档