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

时间:2017-06-11 18:48:32

标签: php arrays multidimensional-array

我有一个api,它返回前端请求的图像路径。但在Linux服务器中,它需要自然排序。我想在yol键中自然排序值。

$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);
});

输出:

enter image description here

1 个答案:

答案 0 :(得分:1)

看看PHP sort functionsnatsort应该适合您的情况。

只需将以下行更改为:

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

也是一个简单的代码示例:

    $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'
    // ]
相关问题