我有一个php会话$ _SESSION ['details']当我print_r返回以下内容时。
Array (
[0] => Array ( [nameID] => 1357325402 [vol] => 1 [catID] => 2 )
[1] => Array ( [nameID] => 1357325423 [vol] => 1 [catID] => 2 )
[2] => Array ( [nameID] => 1357325470 [vol] => 1 [catID] => 10 )
[3] => Array ( [nameID] => 1357325440 [vol] => 1 [catID] => 4 )
[4] => Array ( [nameID] => 1357325416 [vol] => 1 [catID] => 2 )
[5] => Array ( [nameID] => 1357325471 [vol] => 1 [catID] => 10 )
)
如何对数组进行排序以使所有catID在一起?
Array (
[0] => Array ( [nameID] => 1357325402 [vol] => 1 [catID] => 2 )
[1] => Array ( [nameID] => 1357325423 [vol] => 1 [catID] => 2 )
[2] => Array ( [nameID] => 1357325416 [vol] => 1 [catID] => 2 )
[3] => Array ( [nameID] => 1357325440 [vol] => 1 [catID] => 4 )
[4] => Array ( [nameID] => 1357325470 [vol] => 1 [catID] => 10 )
[5] => Array ( [nameID] => 1357325471 [vol] => 1 [catID] => 10 )
)
答案 0 :(得分:1)
试试这个:
function cmp($a, $b){
return strcmp($a["catID"], $b["catID"]);
}
usort($_SESSION['details'], "cmp");
答案 1 :(得分:0)
您可以使用PHP的usort功能并提供您自己的比较功能。像这样:
function cmp($a, $b)
{
return ($a["catID"] > $b["catID"]) ? 1 : -1; // Ascending order
}
usort($_SESSION[‘details’], "cmp");