首页 > 解决方案 > 读取目录不在文件系统顺序中

问题描述

我有这个代码:

 if (is_dir($dir)) {
            if ($dh = opendir($dir)) {
                while (($file = readdir($dh)) !== false) {
                    print_r(" ".$file. " ");
                    $data = array(
                        'emplacement' => 'uploads/slidesFiles/'.$file
                    );
                    $jpgId = $CI->dataaccess::InsertJpg($data);
                    $data = array(
                        'idSlideZip' => $zipId,
                        'idSlideJpg' =>$jpgId
                    );
                    $CI->dataaccess::InsertJpgToSlide($data);
                }
                closedir($dh);
            }
        }

我的文件夹:

文件夹顺序

但是当我使用 read dir 函数循环进入我的文件夹时,我没有预期的顺序是下一个:

读取目录顺序

我尝试使用排序功能重新排序,但这不是预期的结果。

排序

任何想法 ?

标签: phpcodeigniter

解决方案


看起来您正在遭受字典排序的困扰。

你需要的是natsort()

按字母顺序:

1, 10, 2, 3

纳特索特:

1, 2, 3, 10

这样做的原因是因为数组中的元素被视为字符串,因此字符串中的每个字符都被视为字符而不是整数。

进一步阅读。


推荐阅读