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 DSP28335AD采集实验
  • ¥15 在winods上用C# 怎么编写CAN通信在linux运行
  • ¥15 关于螺旋焊缝的图像处理
  • ¥15 qt 散点图自定义图形
  • ¥100 将USDZ文件转化为带颜色的OBJ文件
  • ¥15 对象代号: , 表单: 不存在!
  • ¥15 WebSocket的问题
  • ¥15 centos上启动kylin后网页报错404
  • ¥20 使用hackrf进行信号收发时接收到的信号幅度太小
  • ¥15 WebSocket的问题