对数字进行排序

时间:2016-03-09 17:21:12

标签: php arrays sorting

我想根据文件名按升序对数组进行排序,这是一个数字。这是有问题的。您会注意到排序是按文件名的第一个数字进行的。是否有预先构建的PHP函数来处理这个?

sort(), ksort()给出与以下相同的结果

Array
(
    [0] => /myPath/folderFoo/1.jpg
    [1] => /myPath/folderFoo/11.jpg
    [2] => /myPath/folderFoo/12.jpg
    [3] => /myPath/folderFoo/13.jpg
    [4] => /myPath/folderFoo/14.jpg
    [5] => /myPath/folderFoo/15.jpg
    [6] => /myPath/folderFoo/16.jpg
    [7] => /myPath/folderFoo/17.jpg
    [8] => /myPath/folderFoo/18.jpg
    [9] => /myPath/folderFoo/199.jpg
    [10] => /myPath/folderFoo/2.jpg
    [11] => /myPath/folderFoo/3.jpg
    [12] => /myPath/folderFoo/4.jpg
    [13] => /myPath/folderFoo/5.jpg
    [14] => /myPath/folderFoo/6.jpg
    [15] => /myPath/folderFoo/7.jpg
    [16] => /myPath/folderFoo/8.jpg
    [17] => /myPath/folderFoo/9.jpg
)

2 个答案:

答案 0 :(得分:1)

正如Mark Ba​​ker建议的那样,您可以使用以下任何一种: - natsort()sort() with SORT_NATURAL flag

<?php
$data = Array
(
    '0' => '/myPath/folderFoo/1.jpg',
    '1' => '/myPath/folderFoo/11.jpg',
    '2' => '/myPath/folderFoo/12.jpg',
    '3' => '/myPath/folderFoo/13.jpg',
    '4' => '/myPath/folderFoo/14.jpg',
    '5' => '/myPath/folderFoo/15.jpg',
    '6' => '/myPath/folderFoo/16.jpg',
    '7' => '/myPath/folderFoo/17.jpg',
    '8' => '/myPath/folderFoo/18.jpg',
    '9' => '/myPath/folderFoo/199.jpg',
    '10' => '/myPath/folderFoo/2.jpg',
    '11' => '/myPath/folderFoo/3.jpg',
    '12' => '/myPath/folderFoo/4.jpg',
    '13' => '/myPath/folderFoo/5.jpg',
    '14' => '/myPath/folderFoo/6.jpg',
    '15' => '/myPath/folderFoo/7.jpg',
    '16' => '/myPath/folderFoo/8.jpg',
    '17' => '/myPath/folderFoo/9.jpg'
);
natsort($data);
echo "<pre/>";print_r($data);
$data1 = Array
(
    '0' => '/myPath/folderFoo/1.jpg',
    '1' => '/myPath/folderFoo/11.jpg',
    '2' => '/myPath/folderFoo/12.jpg',
    '3' => '/myPath/folderFoo/13.jpg',
    '4' => '/myPath/folderFoo/14.jpg',
    '5' => '/myPath/folderFoo/15.jpg',
    '6' => '/myPath/folderFoo/16.jpg',
    '7' => '/myPath/folderFoo/17.jpg',
    '8' => '/myPath/folderFoo/18.jpg',
    '9' => '/myPath/folderFoo/199.jpg',
    '10' => '/myPath/folderFoo/2.jpg',
    '11' => '/myPath/folderFoo/3.jpg',
    '12' => '/myPath/folderFoo/4.jpg',
    '13' => '/myPath/folderFoo/5.jpg',
    '14' => '/myPath/folderFoo/6.jpg',
    '15' => '/myPath/folderFoo/7.jpg',
    '16' => '/myPath/folderFoo/8.jpg',
    '17' => '/myPath/folderFoo/9.jpg'
);
sort($data1,SORT_NATURAL);
echo "<pre/>";print_r($data1);
?>

输出: - https://eval.in/533491

供参考: -

http://www.php.net/manual/en/function.natsort.php

http://www.php.net/manual/en/function.sort.php

答案 1 :(得分:0)

无需更改阵列,也可以尝试此操作。

ID     Assigned To            Location
1      2                      2
  

输出:

$arr = array("/myPath/folderFoo/1.jpg",
    "/myPath/folderFoo/11.jpg",
    "/myPath/folderFoo/12.jpg",
    "/myPath/folderFoo/13.jpg",
    "/myPath/folderFoo/14.jpg",
    "/myPath/folderFoo/15.jpg",
    "/myPath/folderFoo/16.jpg",
    "/myPath/folderFoo/17.jpg",
    "/myPath/folderFoo/18.jpg",
    "/myPath/folderFoo/199.jpg",
    "/myPath/folderFoo/2.jpg",
    "/myPath/folderFoo/3.jpg",
    "/myPath/folderFoo/4.jpg",
    "/myPath/folderFoo/5.jpg",
    "/myPath/folderFoo/6.jpg",
    "/myPath/folderFoo/7.jpg",
    "/myPath/folderFoo/8.jpg",
    "/myPath/folderFoo/9.jpg"
);
echo '<pre>';
for($i = 0; $i < count($arr); $i++){
  $int = filter_var($arr[$i], FILTER_SANITIZE_NUMBER_INT);
  $out[$int] = $arr[$i];
}
natsort($out);
print_r($out);
echo '</pre>';