2012-07-27 49 views
2

沒有什麼太多的解釋,我會直接到達樣本代碼:PHP排序多維數組的漁政船

<?php 
$array[0]['a'] = 3; 
$array[0]['b'] = 2; 
$array[0]['c'] = 4; 
$array[0]['d'] = 1; 

$array[1]['a'] = 4; 
$array[1]['b'] = 2; 
$array[1]['c'] = 3; 
$array[1]['d'] = 1; 

$array[2]['a'] = 1; 
$array[2]['b'] = 2; 
$array[2]['c'] = 4; 
$array[2]['d'] = 3; 
?> 

排序後,它應該看起來像:

<?php 

$array[0]['d'] = 1; 
$array[0]['b'] = 2; 
$array[0]['a'] = 3; 
$array[0]['c'] = 4; 


$array[1]['d'] = 1; 
$array[1]['b'] = 2; 
$array[1]['c'] = 3; 
$array[1]['a'] = 4; 

$array[2]['a'] = 1; 
$array[2]['b'] = 2; 
$array[2]['d'] = 3; 
$array[2]['c'] = 4; 
?> 

可以」真的讓我的頭在附近。簡而言之:我不想排序整個數組,但我想對數組的特定維數進行排序

回答

0

使用foreach降低一級,然後對每個子數組進行排序。還有,你可以做到這一點的幾種方法:

foreach($arr as &$sub) asort($sub); 
// OR 
foreach($arr as $k=>$sub) asort($arr[$k]); 
// OR 
foreach(array_keys($arr) as $k) asort($arr[$k]); 
0

你必須直接與你進行排序指數:

ksort($array[0]);

排序的所有索引:

foreach($array as $index => $val){ 
    asort($val); 
    $array[$index] = $val; 
}