2012-02-14 188 views
0

例如,在php中对多维数组排序 - 按值排序

我有以下数组设置,每个美国主要城市都有其人口规模。

$usapopstats = array(
    array('New York',8008278), 
    array('Los Angeles',3694820), 
    array('Chicago',2896016), 
    array('Houston',1953631), 
    array('Philadelphia',1517550), 
    array('Phonenix',45), 
    array('San Diego',1223400), 
    array('Dallas',1188580), 
    array('San Antonio',1144646), 
    array('Detroit',951270) 
); 

我想按这些信息的种群大小排序。但是当我尝试使用arsort函数时,它会根据关键数据对数组进行排序,而不是按城市排序的值数据。

所以我的问题是你怎么编程这种类型的多维数组的人口规模排序?有任何想法吗?

如果数组被改写这样

$usapopstats = array(
    'New York'=>8008278, 
    'Los Angeles'=>3694820, 
    'Chicago'=>2896016, 
    'Houston'=>1953631, 
    'Philadelphia'=>1517550, 
    'Phoenix'=>45, 
    'San Diego'=>1223400, 
    'Dallas'=>1188580, 
    'San Antonio'=>1144646, 
    'Detroit'=>951270 
); 
asort($usapopstats); 

这将通过人口规模排序的数组。

+1

可能重复[在PHP中对多维数组排序](http://stackoverflow.com/questions/2059255/sorting-multidimensional-array-in-php) – jprofitt 2012-02-14 13:03:53

+0

也许只是操纵你的嵌套数组到一个简单的键值数组和'asort()' – 2012-02-14 13:07:29

+0

usort() - http://www.php.net/manual/en/function.usort.php – 2012-02-14 13:09:55

回答

1

你需要创建一个用户排序功能(这是最美丽和最快速的编程解决方案:

$usapopstats = array(
    array('New York',8008278), 
    array('Los Angeles',3694820), 
    array('Chicago',2896016), 
    array('Houston',1953631), 
    array('Philadelphia',1517550), 
    array('Phonenix',45), 
    array('San Diego',1223400), 
    array('Dallas',1188580), 
    array('San Antonio',1144646), 
    array('Detroit',951270) 
); 

function sort_helper ($a, $b) { 
    if ($a[1] > $b[1]) return 1; 
    if ($a[1] == $b[1]) return 0; 
    return -1; 
} 

usort ($usapopstats, sort_helper); 
var_dump($usapopstats); 

这不是最快的代码,罚款说多达1000条记录的名单,但我不会这样做一个有100,000个条目的数组,因为每次比较都会调用sort_helper函数,并且由于n个log n比较是必要的,所以这意味着同样多的函数调用。一个长列表,编码密钥中的人口和ksort:

$usapopstats = array(
    array('New York',8008278), 
    array('Los Angeles',3694820), 
    array('Chicago',2896016), 
    array('Houston',1953631), 
    array('Philadelphia',1517550), 
    array('Phonenix',45), 
    array('San Diego',1223400), 
    array('Dallas',1188580), 
    array('San Antonio',1144646), 
    array('Detroit',951270) 
); 

$statshelper = array(); 
foreach($usapopstats as $index=>$stat){ 
    $statshelper[$stat[1]."_".$index] = $stat; //also add the index to the key to avoid duplicates 
} 

ksort($statshelper, SORT_NUMERIC); //because the keys are strings (they contain an underscore) by default it will compare lexographically. The flag enforces numerical comparison on the part that can be converted to a number (i.e. the population) 
$usapopstats = array_values($statshelper); 

var_dump($usapopstats); 
+0

谢谢克劳德。您的解决方案很完善。虽然我需要一段时间才能理解你的ksort代码算法部分。自大学时代以来,我一直没有触及数据排序算法 - 就像十年前一样!因此,我有点慢,以捡起来。 我没有得到的是,如果array_values函数应该将statshelper数组的值作为$ usapopstats数组的新数组返回,是不是会覆盖usapopstats数组的键/值对? – awongCM 2012-02-15 12:58:06

+0

不客气:)。 $ statshelper数组的值中将包含所有正确的数据,但只需要在键中进行排序即可使用一些“垃圾”。所以'$ usapopstats = array_values($ statshelper)'这行只是为了摆脱这个垃圾。您也可以转储$ statshelper变量以查看差异。 – Claude 2012-02-15 13:07:07