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

报告相同问题?

悬赏问题

  • ¥200 关于#c++#的问题,请各位专家解答!
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥15 绘制多分类任务的roc曲线时只画出了一类的roc,其它的auc显示为nan
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀