PHP:根据字母顺序将数组中的值分开 [英] PHP: separate value in array according to alphabate order

查看:102
本文介绍了PHP:根据字母顺序将数组中的值分开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想根据alphabate在数组中查找值,并希望根据alphabate顺序在数组值中列出。

i want to find values in array according to alphabate and want to make list in of array values according to alphabate order.

我的数组是这样的:

my array is like that:


数组(
[0] =>数组

[0] =>阿迪达斯
[1] => AKG
[2] =>苹果
[3] =>廉价产品
[4] =>佳能
[5] =>戴尔
[6] => Dixons
[7] => HTC
[8] =>利物浦
[9] =>微软
[10] =>倍耐力轮胎
[11] =>

Array ( [0] => Array ( [0] => Adidas [1] => AKG [2] => Apple [3] => Barrats [4] => Canon [5] => Dell [6] => Dixons [7] => HTC [8] => Liverpool [9] => Microsoft [10] => Pirelli Tyres [11] => )

并且我想按照这样的alphabate列出值列表:

and i want to make a list of values according to alphabate like this:

A           
________        
  Adidas       
  AKG 

plz有什么想法吗?

plz any idea?

推荐答案

可以使用som优化,但确实符合您的要求:

Could use some optimization, but does exactly what you asked:

    $a = array("Chicken","Fish","Hello","Lebowski","What","hey","foo");

    asort($a);
    $alphaArr = array();

    foreach(range('A', 'Z') as $letter) {

        // create empty array at offset for current letter
        $alphaArr[$letter] = array();

        foreach($a as $item) {

            // if the first letter starts with current letter
            // push it into subarray
            if (substr(strtolower($item),0,1) == strtolower($letter)) {
                $alphaArr[$letter][] = $item;
            }
        }
    }
    print_r($alphaArr);

这篇关于PHP:根据字母顺序将数组中的值分开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆