-5

I'm trying to sort the array in PHP. Please see my array:

Array
(
    [0] => Array
        (
            [id] => 8
            [date_start_month_name] => January
            [approved] => Accepted: yes
        )

    [1] => Array
        (
            [id] => 3
            [date_start_month_name] => January
            [approved] => Accepted: yes
        )

    [2] => Array
        (
            [id] => 2
            [date_start_month_name] => March
            [approved] => Accepted: yes
        )

    [3] => Array
        (
            [id] => 5
            [date_start_month_name] => April
            [approved] => Accepted: yes
        )

)

I want to sort the array using month name, for example: January and get something like this:

Array
(
    [0] => Array
        (
            [id] => 8
            [date_start_month_name] => January
            [approved] => Accepted: yes
        )

    [1] => Array
        (
            [id] => 3
            [date_start_month_name] => January
            [approved] => Accepted: yes
        )
)

How can I do that? Thanks in advance for any help. Best!

Jakub
  • 255
  • 1
  • 2
  • 15

2 Answers2

0

use usort

usort($array, function($a, $b) {
    return $a['date_start_month_name'] > $b['date_start_month_name']
});
jasir
  • 1,473
  • 11
  • 28
0

You can use the usort function to acheive this like so:

<?php
     // Sort the multidimensional array
     usort($results, "custom_sort");
     // Define the custom sort function
     function custom_sort($a,$b) {
          return $a['some_sub_var']>$b['some_sub_var'];
     }
?>
Rob Stevenson-Leggett
  • 34,539
  • 21
  • 86
  • 139