2012-02-14 108 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