duanchuo7741 2018-11-03 11:40
浏览 196
已采纳

在PHP中按字母顺序排序数组

I'm creating a website for a book publishing company. They've books in multiple different languages and want to display some details for each language. To retrieve all the necessary information for each language, I do the following:

  <div class="col-lg-3" align="center">
        <?php
            $fields = get_field_objects();                  
                if ($fields):
                    foreach ($fields as $name => $field):    
                        if (stripos($name, 'isbn') !== false) : ?>
                            <?php $lang = substr($field['name'], strpos($field['name'], "_")); ?>
                            <div class="panel-group">
                                <div class="panel panel-default">
                                    <div class="panel-heading">
                                        <h4 class="panel-title">
                                            <a data-toggle="collapse" href="#collapse1<?php echo $lang ?>">Deutsch/<?php echo substr($field['label'], strpos($field['label'], " ") + 1); ?></a>
                                        </h4>
                                    </div>
                                    <div id="collapse1<?php echo $lang ?>" class="panel-collapse collapse">
                                        <div class="panel-body">
                                      // ... get all neccessary information ...
                                       </div>
                                    </div>
                                </div>
                            </div>
                        <?php endif;
                    endforeach;
                endif; ?>
            </div>

Now my problem is, that if somebody creates a book, forgets to add a language and wants to add it after the book has been saved to the database, the alphabetical order is not correct anymore. Therefore I'd like to add a sorting function for the $fields array.

sort($fields) doesn't work since after doing this, everything is blank.

Here's a screenshot of how the output looks like. enter image description here

And sometimes, the order is wrong (e.g. Deutsch/English appears on the bottom). So I'll have to sort the part where I add the second language (next to German). The part where I add this is here:

 <h4 class="panel-title">
    <a data-toggle="collapse" href="#collapse1<?php echo $lang ?>">Deutsch/<?php echo substr($field['label'], strpos($field['label'], " ") + 1); ?></a>
 </h4>

Does anyone have any ideas? Please let me know if code is missing!

P.S. the plugin I use is "Advanced-Custom-Fields" and the functions like e.g. "get_field_objects()" come with that plugin!

  • 写回答

2条回答 默认 最新

  • dongshi1880 2018-12-12 09:51
    关注

    So what solve my problem is @LeoTahk 's suggestion of adding ksort().

    Final code:

        <div class="col-lg-3" align="center">
          <?php
             $fields = get_field_objects();
    //working solution
             ksort($fields);
    
             if($fields):
               ....
             endif; ?>
         </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微信小程序 用oss下载 aliyun-oss-sdk-6.18.0.min client报错
  • ¥15 ArcGIS批量裁剪
  • ¥15 labview程序设计
  • ¥15 为什么在配置Linux系统的时候执行脚本总是出现E: Failed to fetch http:L/cn.archive.ubuntu.com
  • ¥15 Cloudreve保存用户组存储空间大小时报错
  • ¥15 伪标签为什么不能作为弱监督语义分割的结果?
  • ¥15 编一个判断一个区间范围内的数字的个位数的立方和是否等于其本身的程序在输入第1组数据后卡住了(语言-c语言)
  • ¥15 Mac版Fiddler Everywhere4.0.1提示强制更新
  • ¥15 android 集成sentry上报时报错。
  • ¥15 抖音看过的视频,缓存在哪个文件