0

I created separated arrays for admins:

$admins[] = array(
                'username' => 'john',
                'admin' => '1',
                'level' => '1'
            );
$admins[] = array(
                'username' => 'adam',
                'admin' => '1',
                'level' => '1'
            );
$admins[] = array(
                    'username' => 'ana',
                    'admin' => '1',
                    'level' => '2'
                );

Now, I want to get all the usernames show in a select option element. But the usernames should arrange alphabetically. What I did was I combined all of admin arrays into one multidimensional arrays then I created a function to sort the arrays by username.

$items = array();
foreach($admins as $username) {
    $items[] = $username;
}
#echo "<pre>";
#print_r($items);

function sortByName($a, $b) {
    return $a['username'] - $b['username'];
}
usort($items, 'sortByName');

After that, I tried to show it in the select option element. But the usernames are not arranged.

<select name="support-name">
                <option value="" required>select</option>
                <?
                foreach ($admins as $admin){?>
                     <option value="<?=$admin['username']?>"><?=ucwords($admin['username'])?></option>
                        <?}
                    ?>

                <?}
                ?>
            </select>
xkev
  • 17
  • 5
  • https://stackoverflow.com/questions/2699086/how-to-sort-multi-dimensional-array-by-value try this – Wasif Khalil Jan 08 '19 at 04:07
  • Possible duplicate of [How to Sort Multi-dimensional Array by Value?](https://stackoverflow.com/questions/2699086/how-to-sort-multi-dimensional-array-by-value) – Nick Jan 08 '19 at 04:22
  • I think it is not a duplicate, because the answers here https://stackoverflow.com/questions/2699086/how-to-sort-multi-dimensional-array-by-value do not answer my question in my case. – xkev Jan 08 '19 at 04:34

4 Answers4

1

You can use strcmp()

The strcmp() function compares two strings.and it is binary-safe and case-sensitive.

Example -

 function sortByOrder($a, $b) {
        return strcmp($a['username'], $b['username']);
    }

Try This

$admins[] = array(
                'username' => 'john',
                'admin' => '1',
                'level' => '1'
            );
$admins[] = array(
                'username' => 'adam',
                'admin' => '1',
                'level' => '1'
            );
$admins[] = array(
                    'username' => 'ana',
                    'admin' => '1',
                    'level' => '2'
                );

function sortByOrder($a, $b) {
    return strcmp($a['username'], $b['username']);
}

usort($admins, 'sortByOrder');

Form Code

<select name="support-name">
        <option value="" required>select</option>
        <?
        foreach ($admins as $admin){?>
             <option value="<?=$admin['username']?>"><?=ucwords($admin['username'])?></option>
                <?}
            ?>

</select>
shubham715
  • 3,324
  • 1
  • 17
  • 27
0

here is the simple method

<?php
  $admins[] = array(
                'username' => 'john',
                'admin' => '1',
                'level' => '1'
            );
$admins[] = array(
                'username' => 'adam',
                'admin' => '1',
                'level' => '1'
            );
$admins[] = array(
                    'username' => 'ana',
                    'admin' => '1',
                    'level' => '2'
                );

$items = array();

foreach($admins as $username) {
    $items[] = $username['username'];
}
?>
    // here the display
<html>
<body>
  <select name="support-name">
        <option value="" required>select</option>
        <?php
        sort($items); 
        foreach($items as $val){
         echo "<option value=''>".ucwords($val)."</option>";
        }
        ?>
    </select>
 </body>
 </html>
Nirina-aj
  • 192
  • 1
  • 3
  • 18
0

If all you need is a list of usernames, while discarding the other keys in each admin_structure, you can create a new array $usernames_for_html and populate it within your very first foreach, and then asort() that new array. That creates a situation where the new (1 dimensional) array only contains sorted usernames, which you can format/sanitize to your needs.

$usernames_for_html = array();

foreach($admins AS $user){
   $usernames_for_html[] = ucwords($user['username']); //SANITIZE TO YOUR NEED
}

asort($usernames_for_html);
Biospy
  • 26
  • 3
0

I think you all missed the array_multisort method ! I have created an example here.

$arr[1][0] = "b";
$arr[2][0] = "E";
$arr[3][0] = "A";

$arr[1][1] = "C";
$arr[2][1] = "A";
$arr[3][1] = "D";

$arr[1][2] = "E";
$arr[2][2] = "C";
$arr[3][2] = "E";

$arr[1][3] = "d";
$arr[2][3] = "B";
$arr[3][3] = "C";

$arr[1][4] = "A";
$arr[2][4] = "D";
$arr[3][4] = "B";



array_multisort($arr[1], SORT_NATURAL | SORT_FLAG_CASE, SORT_ASC, 
            $arr[2], SORT_NATURAL, SORT_ASC,
            $arr[3], SORT_NATURAL, SORT_ASC);

var_dump($arr);
Thanasis
  • 329
  • 4
  • 8