dongpai9986 2017-06-11 18:48
浏览 41
已采纳

自然排序到PHP中的多维数组

I have a api that returns requested images paths to front end. But in Linux server its needs natural sorting. I want to natural sort values in the yol key.

$klein->respond('GET', '/getPaths/[:name]', function($request,$response) {

$path00 = "../manga/" . $request->name;

function getAll($path00)
{
$dirs = [];
foreach (new DirectoryIterator($path00) as $item) {
    if (!$item->isDir() || $item->isDot()) {
        continue;
    }
    $gedo = [];
    $path01  = $path00 . "/" . $item;
    $path02 = substr($path01, 3);
    $yol = new DirectoryIterator($path01);
    foreach ($yol as $esya) {
        if (!$esya->isDot() && $esya->getFilename() !== ".DS_Store") {
            $gedo[] = $path02 . "/" .  $esya->getFilename();

        }
    }

    $dirs[] = array('klasor' => $item->getFilename(), 'yol' => $gedo);
}

return $dirs; // Output its in the image.
};

$data = getAll($path00);
$response->json($data);
});

Output:

enter image description here

  • 写回答

1条回答 默认 最新

  • dongqiang1226 2017-06-11 19:14
    关注

    Take a look at PHP sort functions. natsort should work for your case.

    Simply change the following line to this:

        natsort($gedo);
        $dirs[] = array('klasor' => $item->getFilename(), 'yol' => $gedo);
    

    Also a simple code example:

        $input = [
            'abc' => [
                'item1',
                'item4',
                'item2'
            ]
        ];
    
        $output = natsort($input['abc']); // $output will be true
        // but $input is now sorted and looking like:
        // 'abc' => [
        //     'item1',
        //     'item2',
        //     'item4'
        // ]
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 在若依框架下实现人脸识别
  • ¥15 网络科学导论,网络控制
  • ¥100 安卓tv程序连接SQLSERVER2008问题
  • ¥15 利用Sentinel-2和Landsat8做一个水库的长时序NDVI的对比,为什么Snetinel-2计算的结果最小值特别小,而Lansat8就很平均
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同