按字母顺序显示商店名称 - 字母 - php

时间:2015-08-05 14:34:08

标签: php arrays

我想按字母顺序显示商店名称。我有一个商店桌子,我在那里存储有关商店的所有细节,包括其名称,标识,描述等。我希望名称将以字母顺序显示。如示例所示。并点击[A] [B] [C] .... [Z]

显示商店
Array
(
    [1] => Array
        (
            [0] => 100bestbuy.com
        )

    [A] => Array
        (
            [0] => Adlabsimagica
            [1] => Airasiago
            [2] => Airtel                
        )

    [B] => Array
        (
            [0] => Babyhugz
            [1] => Babyoye
            [2] => Bagskart
            [3] => Basicslife
            [4] => Bata             
        )

    [C] => Array
        (
            [0] => Clifton
            [1] => Coke2Home
            [2] => Condompoint
            [3] => Croma
        )

    [D] => Array
        (
            [0] => Dabur
            [1] => Dealofthedayindia.com
            [2] => Dhamaal
            [3] => Dominos
        )

输出:

{{1}}

1 个答案:

答案 0 :(得分:0)

您可以使用其他阵列来保存您所处的位置 这样的事情:

<?php 
    $temp = array(); // would also generate a dynamic array
    $position = array();
    foreach (range('0', '9') as $i) {
        $position [$i] = 0;
    }
    foreach (range('a', 'z') as $i) {
        $position [$i] = 0;
    }

    $result = mysql_query("SELECT LEFT(`name_en-GB`, 1 ) AS FirstLetter, `name_en-GB` FROM `store` ORDER BY FirstLetter, `name_en-GB`");
    while ($row = mysql_fetch_array($result)) {
            $pos = $position[$row['FirstLetter']];
            $position ['FirstLetter'] ++;
            $temp[$row['FirstLetter']][$pos] = $row['name_en-GB'];
    }  
?>

现在你有一个阵列在子阵列中的位置 应该工作,只需要小心地将所有的首字母大写/小写或验证之前