0
Array
(
    [0] => Array
        (
            [sales] => 117513.00000000
            [month] => 1
            [month_name] => January
            [year] => 2018
        )

    [1] => Array
        (
            [purchases] => 136350.00000000
            [month] => 9
            [month_name] => September
            [year] => 2017
        )

    [2] => Array
        (
            [sales] => 23025.00000000
            [month] => 11
            [month_name] => November
            [year] => 2017
        )

    [3] => Array
        (
            [sales] => 6447.00000000
            [month] => 12
            [month_name] => December
            [year] => 2017
        )

)

Here I already sorted the array by year wise , but I need to sort by month descending .

Array
(

 [0] => Array
        (
            [sales] => 117513.00000000
            [month] => 1
            [month_name] => January
            [year] => 2018
        )

    [1] => Array
        (
            [purchases] => 6447.00000000
            [month] => 12
            [month_name] => December
            [year] => 2017
        )

    [2] => Array
        (
            [sales] => 23025.00000000
            [month] => 11
            [month_name] => November
            [year] => 2017
        )

    [3] => Array
        (
            [sales] => 136350.00000000
            [month] => 9
            [month_name] => September
            [year] => 2017
        )

)

How can I get the second format , which is sorted by month..

Regolith
  • 2,944
  • 9
  • 33
  • 50
sradha
  • 2,216
  • 1
  • 28
  • 48

1 Answers1

0

Use array_multisort function to do this.

<?php

// Obtain a list of columns
foreach ($data as $key => $row) {
  $month[$key]  = $row['month'];
}


array_multisort($month, SORT_DESC);
?>
Viraj Amarasinghe
  • 911
  • 10
  • 20