希望下面的代码块可以帮助您。请仔细阅读评论。代码块-
// Your previous code.
// Say this is your $oder_terms variable
$order_terms = array(
\'Sanchez\' => \'Sanchez Link\',
\'Smith\' => \'Smith Link\',
\'Dramatist\' => \'Dramatist Link\',
\'Rashed\' => \'Rashed Link\',
\'Munez\' => \'Munez Link\',
\'James\' => \'James Link\',
\'Jacky\' => \'Jacky Link\',
);
ksort($order_terms);
// After ksort($order_terms); we get below array
/*
Array
(
[Dramatist] => Dramatist Link
[Jacky] => Jacky Link
[James] => James Link
[Munez] => Munez Link
[Rashed] => Rashed Link
[Sanchez] => Sanchez Link
[Smith] => Smith Link
)
*/
// Now we need to group them on the basis of alphabet. Right ?
$new_order_terms = array();
foreach($order_terms as $key => $value) {
$firstLetter = substr($value, 0, 1);
$new_order_terms[$firstLetter][$key] = $value;
}
// Now if you do print_r($new_order_terms); your output will be
/*
Array
(
[D] => Array
(
[Dramatist] => Dramatist Link
)
[J] => Array
(
[Jacky] => Jacky Link
[James] => James Link
)
[M] => Array
(
[Munez] => Munez Link
)
[R] => Array
(
[Rashed] => Rashed Link
)
[S] => Array
(
[Sanchez] => Sanchez Link
[Smith] => Smith Link
)
)
*/
// All grouped by their first letter.