简体   繁体   English

组数组结果按字母顺序排列

[英]Group array results in Alphabetic order PHP

I'm using below code to display Image & Name of webisites form database. 我正在使用下面的代码来显示Webisites表单数据库的图像和名称。

<fieldset>  
    <h1>A</h1>          
    <ul>
        <?php foreach ($records as $key) { ?>
        <li class="siteli"> <a href="#" class="add">        
            <div id="site-icon"><img src="<?php echo $key->site_img; ?>" width="16" height=""></div>
            <p id="text-site"> <?php echo $key->site_name; ?></p>
        </li>
        <?php } ?>
    </ul>
</fieldset>

Now I'm trying to group this results alphabetically by adding A, B, C etc as title. 现在,我尝试通过添加A,B,C等作为标题按字母顺序对结果进行分组。

Example, 例,

A    
Amazon     
Aol    
Aol Mail

B    
Bing     
Bogger

You can use array sorting to sort the array. 您可以使用数组排序数组进行排序。 In your case I would choose sort() 在你的情况下,我会选择sort()

Now to show the links with a preceding Letter I would use: 现在,我将使用前面的字母显示链接:

<?php
$records = ['Aaaaa', 'Aaaa2', 'bbb', 'bbb2', 'Dddd'];
$lastChar = '';
sort($records, SORT_STRING | SORT_FLAG_CASE); //the flags are needed. Without the `Ddd` will come before `bbb`.
//Available from version 5.4. If you have an earlier version (4+) you can try natcasesort()

foreach($records as $val) {
  $char = $val[0]; //first char

  if ($char !== $lastChar) {
    if ($lastChar !== '')
      echo '<br>';

    echo strtoupper($char).'<br>'; //print A / B / C etc
    $lastChar = $char;
  }

 echo $val.'<br>';
}
?>

This will output something like 这将输出类似

A
Aaaa2
Aaaaa

B
bbb
bbb2

D
Dddd

Notice that the C is missing, because there are no words starting with C. 请注意,C丢失了,因为没有以C开头的单词。

This is a modification of @Hugo Delsing's answer. 这是@Hugo Delsing答案的修改。

I needed to be able to group my brands together like this, but one bug in the code was the fact that if I had say iForce Nutrition and Inner Armour it would group these into two separate groups because of the letter case. 我需要能够像这样将我的品牌分组在一起,但是代码中的一个错误是这样一个事实,即如果我说过iForce Nutrition和Inner Armor,由于字母大小写,它们会将它们分为两个单独的组。

Changes I Made 我所做的更改
Because it was fully alphabetical, it was only going by first letter I changes SORT_STRING to SORT_NATURAL 因为它是完全按字母顺序排列的,所以只将第一个字母改为我将SORT_STRING更改为SORT_NATURAL

Changed: if ($char !== $lastChar) to if (strtolower($char) !== strtolower($lastChar)) 更改为:if($ char!== $ lastChar)改为if(strtolower($ char)!== strtolower($ lastChar))

<?php
$records = ['FinaFlex', 'iForce Nutrition', 'Inner Armour', 'Dymatize', 'Ultimate    Nutrition'];
$lastChar = '';
sort($records, SORT_NATURAL | SORT_FLAG_CASE); //Allows for a full comparison and will alphabetize correctly.

foreach($records as $val) {
 $char = $val[0]; //first char

  if (strtolower($char) !== strtolower($lastChar)) {
    if ($lastChar !== '')
     echo '<br>';

     echo strtoupper($char).'<br>'; //print A / B / C etc
     $lastChar = $char;
    }

    echo $val.'<br>';
  }
?>

The final output will be like this 最终的输出将像这样

D
Dymatize

F
FinaFlex

I
iForce Nutrition
Inner Armour

U
Ultimate Nutrition

I hope this helps everyone, and a big thank you to Hugo for providing the code that got me exactly where I needed to be. 我希望这对所有人都有帮助,并非常感谢Hugo提供的代码使我准确地成为了我所需要的地方。

You can see my example here https://hyperionsupps.com/brand-index 您可以在这里查看我的示例https://hyperionsupps.com/brand-index

$records = ['FinaFlex', 'iForce Nutrition', 'Inner Armour', 'Dymatize', 'Ultimate    Nutrition'];

    $temp=array();    
    $first_char="";
    for($i=0;$i<count($records);$i++)
    {
        $first_char= strtoupper ($records[$i][0]);             
             if(!in_array($first_char, $temp))
             {
                 echo strtoupper($first_char).'<br>'; //print A / B / C etc                      

             }
             $temp[]=  $first_char;
            echo $records[$i]."<br>";
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM