我想捕获波斯语单词的第一个字母,然后按波斯语分类为字母顺序。确实,我按波斯语顺序得到了一系列波斯语形式的数据库,我只想将它们放在适当的字母类别中
$results = array();
$persian_words = ['ادب', 'بهنام', 'طلا', 'بانک'];
$prev_label = null;
foreach ($persian_words as $word){
$first_letter = substr($word,0, 1);
if ($first_letter !== $prev_label){
$results[$first_letter][] = $word;
$prev_label = $first_letter;
}else{
$results[$prev_label][] = $word;
}
desired result:
$results = array(
'ا'=>array('ادب'),
'ب' => array('بهنام','بانک'),
'ط' => array('طلا')
);