1

newbie here I have this PHP code and I'm trying to sort in alphabetical order the 2nd option data in a select element. What block of code should I add in the existing code below to achieve this?

 <?php if ( $enmse_de == 1 ) { // SHOW EXPLORER? ?>
    <div class="enmse-selector <?php echo $enmse_ddval ?>">
    <?php if ( $enmse_hsd == 0 ) {  ?>
      <select name="enmse_series" class="enmse_series">
        <option value="0">- <?php echo $enmse_explorerbrowseseries; ?> -</option>
        <?php foreach ($enmse_series as $enmse_s) {  ?>
          <option value="<?php echo '&amp;enmse_sid=' .  $enmse_s->series_id; ?>">
          <?php echo stripslashes($enmse_s->s_title); ?></option>       
        <?php }; ?>    
      </select>
Jeroen Heier
  • 3,520
  • 15
  • 31
  • 32

1 Answers1

0

I think you want to sort the $enmse_series array wtih s_title alphabetically. You can simply achieve this by usning php build-in functins. First you hvae to change the database return array of object to array of array. After that, add this block of code before foreach loop.

// as of PHP 5.5.0 you can use array_column()
$s_title  = array_column($enmse_series, 's_title');

// Sort the data with s_title ascending
// Add $enmse_series as the last parameter, to sort by the common key
array_multisort($s_title, SORT_ASC, $enmse_series);

For more details, referrence php array_multisort

Pyae Phyo Aung
  • 270
  • 1
  • 4
  • 17