我想格式化SQL结果以按第一个字符列出sql条目(从所有数字一起ASC开始,然后从A开始,然后是B,然后是C,依此类推(。 但我也想让每个角色都有自己的部分。
前任:
#
123 Company
21st Century
45th Avenue
99th whatever
A
Aerosmith
Animal
Apple
B
Bat
Binary
Bishop
etc.
如果可能的话,我更愿意在一个查询中执行此操作。 我只是不知道如何设置这样的东西。
下面是一个示例,说明如何遍历数组并检查第一个字符与前一个字符。如果您的 SQL 排序正确,这应该是您所需要的。
$last = '';
foreach(array('12', '33 what', '44 more', 'aa', 'ab', 'b', 'c', 'cd', 'd', 'dd') as $words ){
$current = substr($words, 0, 1);
if(is_numeric($current)) {
$current = '#';
}
if($current != $last) {
echo "n" . strtoupper($current) . "nn";
}
echo $words . "n";
$last = $current;
}
演示:https://eval.in/735196