2011-08-18 123 views
-1

我有以下陣列:如何在PHP中通過鍵對關聯多維數組進行排序?

$data['uploads']['ccc'] = 'something'; 
$data['uploads']['bbb'] = 'something'; 
$data['uploads']['aaa'] = 'something'; 
$data['downloads']['ccc'] = 'something'; 
$data['downloads']['aaa'] = 'something'; 
$data['downloads']['bbb'] = 'something'; 

,我需要它的字母順序對關鍵字排序,所以最終結果必須是:

$data['downloads']['aaa'] = 'something'; 
$data['downloads']['bbb'] = 'something'; 
$data['downloads']['ccc'] = 'something'; 
$data['uploads']['aaa'] = 'something'; 
$data['uploads']['bbb'] = 'something'; 
$data['uploads']['ccc'] = 'something'; 

各種自定義排序功能,沒有運氣。任何想法嘗試?

+0

的可能重複[PHP關聯數組排序(http://stackoverflow.com/questions/2957587/php-associative-array-sort) –

回答

2
ksort($data); 

http://php.net/manual/en/function.ksort.php

從PHP文件中的註釋

另外:

這裏是一個函數遞歸排序多維數組的關鍵:

function deep_ksort(&$arr) { 
    ksort($arr); 
    foreach ($arr as &$a) { 
     if (is_array($a) && !empty($a)) { 
      deep_ksort($a); 
     } 
    } 
} 
+0

此功能將無法運行遞歸...當它返回,只有最外面數組的鍵將被排序。 –

+0

@Christopher Armstrong我剛剛添加了一個函數「deep_keysort」來處理你看到的多維度? – amosrivera

+0

我現在看到它。將我的投票更改爲+1 –

相關問題