看到這個頁面,例如
http://php.net/manual/en/function.sort.php
或例如
<?php
function multisort($array, $sort_by) {
foreach ($array as $key => $value) {
$evalstring = '';
foreach ($sort_by as $sort_field) {
$tmp[$sort_field][$key] = $value[$sort_field];
$evalstring .= '$tmp[\'' . $sort_field . '\'], ';
}
}
$evalstring .= '$array';
$evalstring = 'array_multisort(' . $evalstring . ');';
eval($evalstring);
return $array;
}
$test = array(
array('a' => '1', 'b' => '3'),
array('a' => '2', 'b' => '1'),
array('a' => '1', 'b' => '1'));
echo 'Unsorted: ';
print_r($test);
$result = multisort($test , array('a','b'));
echo 'Sorted: ';
print_r($result);
?>
Result:
Unsorted: Array
(
[0] => Array
(
[a] => 1
[b] => 3
)
[1] => Array
(
[a] => 2
[b] => 1
)
[2] => Array
(
[a] => 1
[b] => 1
)
)
Sorted: Array
(
[0] => Array
(
[a] => 1
[b] => 1
)
[1] => Array
(
[a] => 1
[b] => 3
)
[2] => Array
(
[a] => 2
[b] => 1
)
)
該頁面上的示例#2與您正在嘗試執行的操作非常相似,請查看該示例。 – 2010-04-20 06:09:07
已更新我的答案,正是你想要的。 – codaddict 2010-04-20 06:14:28
哇,非常有幫助的答案。謝謝。 – Evan 2010-04-20 06:17:08