简体   繁体   中英

Sort array alphabetically in 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. 在此处输入图片说明

And sometimes, the order is wrong (eg 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!

PS the plugin I use is "Advanced-Custom-Fields" and the functions like eg "get_field_objects()" come with that plugin!

get_field_objects() returns an array of custom field objects. You can sort it alphabetically using uasort like this:

$fields = get_field_objects();
uasort($fields,"sort_alphabetically");
function sort_alphabetically($a,$b) {
    return $a['name'] > $b['name'];
}

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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