【问题标题】:read dir not in the file system order [duplicate]读取目录不在文件系统顺序中[重复]
【发布时间】:2020-02-11 11:26:18
【问题描述】:

我有这个代码:

 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 函数循环进入我的文件夹时,我没有预期的顺序是下一个:

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

有什么想法吗?

【问题讨论】:

    标签: php codeigniter


    【解决方案1】:

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

    你需要的是natsort()

    按字母顺序:

    1, 10, 2, 3
    

    Natsort:

    1, 2, 3, 10
    

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

    Further reading.

    【讨论】:

    猜你喜欢
    • 2022-01-22
    • 2014-02-12
    • 2022-01-23
    • 2012-09-16
    • 2021-01-06
    • 1970-01-01
    • 2020-11-07
    • 1970-01-01
    相关资源
    最近更新 更多