Heim  >  Artikel  >  php教程  >  按首字母重组数组排序

按首字母重组数组排序

WBOY
WBOYOriginal
2016-09-03 00:00:411362Durchsuche

//压入首字母
        foreach ($res as $k => $v)
        {
            $res[$k]['first'] = strtoupper(substr(trim($v['name']), 0, 1));
            $arr = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z');
            //如果不是字母 把他规划到0-9队列
            if (!in_array($res[$k]['first'], $arr))
            {
                $res[$k]['first'] = '0-9';
            }
        }

        foreach ($res as $k => $v)
        {
            $result[$v['first']][] = $v;
        }
        


        ksort($result);

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn