duanchuo7741 2018-11-03 03: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 01: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条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部