2016-04-25 90 views
0

我和數組,我想按[sys_title]鍵索引升序排列這個數組。我該怎麼辦?PHP數組Ascending排序

[0] => Array 
(
    [sys_id] => 9 
    [sys_title] => Checklist 
    [sys_home] => /cp/system/chl/ 
) 

[1] => Array 
(
    [sys_id] => 8 
    [sys_title] => Bakery Ordering System 
    [sys_home] => /cp/system/bos/ 
) 

預期結果應該是這樣的:

[0] => Array 
(
    [sys_id] => 8 
    [sys_title] => Bakery Ordering System 
    [sys_home] => /cp/system/bos/ 
) 

[1] => Array 
(
    [sys_id] => 9 
    [sys_title] => Checklist 
    [sys_home] => /cp/system/chl/ 
) 
+4

使用usort()函數 – splash58

+0

你能解釋一下嗎? –

+0

http://php.net/manual/en/function.usort.php – splash58

回答

1

你可以試試這段代碼:

usort($data,function($a,$b){ 
    return strcmp($a['sys_title'],$b['sys_title']); 
}); 

print_r($data); 
0

假設你的數組名是$一則:

$tmp = Array(); 
foreach($a as &$ma) { 
    $tmp[] = &$ma["sys_title"]; 
    array_multisort($tmp, $a); 
} 

結果將是

Array 
(
    [0] => Array 
     (
      [sys_id] => 8 
      [sys_title] => Bakery Ordering System 
      [sys_home] => /cp/system/bos/ 
     ) 

    [1] => Array 
     (
      [sys_id] => 9 
      [sys_title] => Checklist 
      [sys_home] => /cp/system/chl/ 
     ) 

)