<bdo id='nfliC'></bdo><ul id='nfliC'></ul>
  1. <i id='nfliC'><tr id='nfliC'><dt id='nfliC'><q id='nfliC'><span id='nfliC'><b id='nfliC'><form id='nfliC'><ins id='nfliC'></ins><ul id='nfliC'></ul><sub id='nfliC'></sub></form><legend id='nfliC'></legend><bdo id='nfliC'><pre id='nfliC'><center id='nfliC'></center></pre></bdo></b><th id='nfliC'></th></span></q></dt></tr></i><div id='nfliC'><tfoot id='nfliC'></tfoot><dl id='nfliC'><fieldset id='nfliC'></fieldset></dl></div>
    <legend id='nfliC'><style id='nfliC'><dir id='nfliC'><q id='nfliC'></q></dir></style></legend>
  2. <tfoot id='nfliC'></tfoot>
    1. <small id='nfliC'></small><noframes id='nfliC'>

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

      时间:2023-09-21
      <i id='7m5aN'><tr id='7m5aN'><dt id='7m5aN'><q id='7m5aN'><span id='7m5aN'><b id='7m5aN'><form id='7m5aN'><ins id='7m5aN'></ins><ul id='7m5aN'></ul><sub id='7m5aN'></sub></form><legend id='7m5aN'></legend><bdo id='7m5aN'><pre id='7m5aN'><center id='7m5aN'></center></pre></bdo></b><th id='7m5aN'></th></span></q></dt></tr></i><div id='7m5aN'><tfoot id='7m5aN'></tfoot><dl id='7m5aN'><fieldset id='7m5aN'></fieldset></dl></div>
      1. <small id='7m5aN'></small><noframes id='7m5aN'>

        1. <tfoot id='7m5aN'></tfoot>
            <bdo id='7m5aN'></bdo><ul id='7m5aN'></ul>
              <legend id='7m5aN'><style id='7m5aN'><dir id='7m5aN'><q id='7m5aN'></q></dir></style></legend>
                <tbody id='7m5aN'></tbody>

                本文介绍了按字母顺序排列的组数组结果 PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我使用下面的代码来显示图像 &网站名称形成数据库.

                I'm using below code to display Image & Name of webisites form database.

                <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>
                

                现在我尝试通过添加 A、B、C 等作为标题,按字母顺序对这些结果进行分组.

                Now I'm trying to group this results alphabetically by adding A, B, C etc as title.

                示例,

                A    
                Amazon     
                Aol    
                Aol Mail
                
                B    
                Bing     
                Bogger
                

                推荐答案

                您可以使用 array排序 对数组进行排序.在你的情况下,我会选择 sort()

                You can use array sorting to sort the array. In your case I would choose 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>';
                }
                ?>
                

                这将输出类似

                A
                Aaaa2
                Aaaaa
                
                B
                bbb
                bbb2
                
                D
                Dddd
                

                请注意缺少 C,因为没有以 C 开头的单词.

                Notice that the C is missing, because there are no words starting with C.

                这篇关于按字母顺序排列的组数组结果 PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:如何在循环遍历数组时将项目添加到数组? 下一篇:PHP:自定义错误处理程序 - 处理解析 &amp;致命错

                相关文章

                最新文章

                  • <bdo id='0g5Oc'></bdo><ul id='0g5Oc'></ul>
                  <tfoot id='0g5Oc'></tfoot>

                    <small id='0g5Oc'></small><noframes id='0g5Oc'>

                    <legend id='0g5Oc'><style id='0g5Oc'><dir id='0g5Oc'><q id='0g5Oc'></q></dir></style></legend>
                    <i id='0g5Oc'><tr id='0g5Oc'><dt id='0g5Oc'><q id='0g5Oc'><span id='0g5Oc'><b id='0g5Oc'><form id='0g5Oc'><ins id='0g5Oc'></ins><ul id='0g5Oc'></ul><sub id='0g5Oc'></sub></form><legend id='0g5Oc'></legend><bdo id='0g5Oc'><pre id='0g5Oc'><center id='0g5Oc'></center></pre></bdo></b><th id='0g5Oc'></th></span></q></dt></tr></i><div id='0g5Oc'><tfoot id='0g5Oc'></tfoot><dl id='0g5Oc'><fieldset id='0g5Oc'></fieldset></dl></div>