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 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题