2013-04-25 235 views
1

我有多維數組,如:PHP:排序多維數組

Array 
(
    [name] => Array 
     (
      [0] => South Africa 
      [1] => Australia 
      [2] => Egypt 
     ) 

    [img] => Array 
     (
      [0] => sa-flag.jpg 
      [2] => au-flag.jpg 
      [1] => eg-flag.jpg 
     ) 
) 

,我想對它進行排序按字母順序,使得其輸出將完全一樣:

Array 
(
    [name] => Array 
     (
      [0] => Australia 
      [1] => Egypt 
      [2] => South Africa 
     ) 

    [img] => Array 
     (
      [0] => au-flag.jpg 
      [2] => eg-flag.jpg 
      [1] => sa-flag.jpg 
     ) 
) 

我做不到能夠對兩個鍵進行排序以使國名與國旗同步。

+4

http://www.php.net/manual/en/ function.array-multisort.php – 2013-04-25 07:50:48

回答

4

請在下面找到解決方案

$kd = array(
'name' => array(
     '0' => 'South Africa', 
     '1' => 'Australia', 
     '2' => 'Egypt', 
    ), 

'img' => array 
    (
     '0' => 'sa-flag.jpg', 
     '2' => 'au-flag.jpg', 
     '1' => 'eg-flag.jpg', 
    ), 
); 
array_multisort($kd['name'], SORT_ASC, SORT_STRING,$kd['img'], SORT_ASC, SORT_STRING); 
echo '<pre>'; 
print_r($kd); 

查找下面的核心理念

http://www.php.net/manual/en/function.array-multisort.php#example-4840

+1

+1不錯的答案,但你也可以添加引用,所以它看起來不錯WER。 – 2013-04-25 08:10:05

0

試試這個

$arr = array_multisort($array);