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'
        // ]
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?