我想让它像:
Imran Omer
Imran Omer
sumair
sumair
sumair
sumair
sumair
wrewr
wrewr
wrewr
wrewr
wrewr
我怎么能这样排序?
这是我正在使用的代码;
if (!in_array($a['sid'],$leave_sids))
{
$struck_off_list_bit .= '
<tr>
<td>'.$a['sid'].'</td>
<td><a href="school_id.php?action=preview_id_card&sid='.$a['sid'].'">'.$a['student_name'].'</a></td>
<td>'.$date.'</td>
<td>'.$student_class.'</td>
<td style="background: #'.$bg_color.'">'.$leave_submitted.'</td>
</tr>
';
}
答案 0 :(得分:1)
好像你正在寻找usort()
。
首先定义一个函数,如
function sortByStudent($a, $b) {
return $a['student_name'] - $b['student_name'];
}
然后,在$a
阵列上调用它:
usort($a, 'sortByStudent');
答案 1 :(得分:1)
你需要使用array_multisort()函数,你的代码应该像这样的代码:
<?php
$students = array(
array('sid' => '38', 'student_name' => 'wrewr'),
array('sid' => '2', 'student_name' => 'Imran Omer'),
array('sid' => '36', 'student_name' => 'sumair'),
array('sid' => '2', 'student_name' => 'Imran Omer'),
array('sid' => '36', 'student_name' => 'sumair'),
array('sid' => '38', 'student_name' => 'wrewr'),
array('sid' => '36', 'student_name' => 'sumair'),
);
$new_array = array();
foreach ($students as $key => $row) {
$new_array[$key] = $row['student_name'];
}
array_multisort($new_array, SORT_ASC, $students);
print_r($students);
?>
答案 2 :(得分:1)
另一个选项
function array_sort($arr, $key, $type = 'asc')
{
$keys = $new_array = array();
foreach ($arr as $k => $v) {
$keys[$k] = $v[$key];
}
if ($type == 'asc') {
asort($keys);
} else {
arsort($keys);
}
reset($keys);
foreach ($keys as $k => $v) {
$new_array[$k] = $arr[$k];
}
return $new_array;
}
使用array_sort($a,'student_name')
答案 3 :(得分:0)
您可以在查询中使用GROUP BY
子句。它会自动对所有结果进行分组