これを行いたい場合は、SQL 内で;
SELECT SUBSTRING(name, 1, 1) as alpha, name from 'user' GROUP BY SUBSTRING(name, 0, 2), name order by 'alpha', 'name'
そしてphpで
<?php
$temp = array(); // would also generate a dynamic array
$result = mysql_query("SELECT SUBSTRING(name, 1, 1) as alpha, name from 'user' GROUP BY SUBSTRING(name, 0, 2), name order by 'alpha', 'name'"
while ($row = mysql_fetch_array($result)) {
$temp[$row['alpha']][] = $row['name'];
}
/* this would create array such as;
'A'
--> 'Adam'
--> 'Apple'
'B'
--> 'Ba...'
--> 'Be...'
*/
?>
お役に立てれば。