4

i am trying to short an array by MONTH name.

[  
   {  
      "order_id":34,
      "user_id":17,
      "sum":65000,
      "month":"May"
   },
   {  
      "order_id":32,
      "user_id":19,
      "sum":15000,
      "month":"July"
   },
   {  
      "order_id":29,
      "user_id":1,
      "sum":20000,
      "month":"April"
   }
]

Is there any way i quickly sort this? And i need the month in name format.

I am expecting a result like below one.

[  
   {  
      "order_id":29,
      "user_id":1,
      "sum":20000,
      "month":"April"
   },
   {  
      "order_id":34,
      "user_id":17,
      "sum":65000,
      "month":"May"
   },
   {  
      "order_id":32,
      "user_id":19,
      "sum":15000,
      "month":"July"
   }
]

I have tried arsort, krsort, array_reverse(), but these methods are not able to short them. So looking for some other solution for this.

Thank you! (in advance)

5
  • Do you want to sort by month (January February March..) or by month name (April August February..) Commented Jul 1, 2016 at 5:51
  • I want to sort by Jan Feb March...i guess reverse_array will work here? Commented Jul 1, 2016 at 5:52
  • No reverse_array() is not working here. Commented Jul 1, 2016 at 5:55
  • Yes its in json format Commented Jul 1, 2016 at 6:06
  • So convert it to normal PHP array first, no? Commented Jul 1, 2016 at 6:10

3 Answers 3

3

Directly any function can not be applied here because your data is in json format,You can achieve it like below:-

<?php

$data = '[  
   {  
      "order_id":34,
      "user_id":17,
      "sum":65000,
      "month":"May"
   },
   {  
      "order_id":32,
      "user_id":19,
      "sum":15000,
      "month":"July"
   },
   {  
      "order_id":29,
      "user_id":1,
      "sum":20000,
      "month":"April"
   }
]';

$new_array = json_decode($data,true); // convert json to php array
echo "<pre/>";print_r($new_array); // print original array

usort($new_array, "compare_months"); // using usort with callback function
var_dump($new_array); // your final sorted array

function compare_months($a, $b) {
    $monthA = date_parse($a['month']);
    $monthB = date_parse($b['month']);

    return $monthA["month"] - $monthB["month"];
}
?>

Output:- https://eval.in/598786

Reference taken:-

PHP re-order array of month names

Sign up to request clarification or add additional context in comments.

Comments

2

Assuming that you have decoded JSON as two-dimensional array you could try to use usort and callback function to compare your month names like this

$json_data = '[  
 {  
  "order_id":34,
  "user_id":17,
  "sum":65000,
  "month":"May"
 },
 {  
   "order_id":32,
   "user_id":19,
   "sum":15000,
   "month":"July"
 },
 {  
  "order_id":29,
  "user_id":1,
  "sum":20000,
  "month":"April"
 }
]';

function cmp_by_month($a, $b)
{
  //Let's compare by month value
  return strtotime($a["month"]) - strtotime($b["month"]);
}

$data = json_decode($json_data);
$result = usort($data, 'cmp_by_month');

2 Comments

Great short and sweet :)
nice thought of using strtotime
1
  • You need to convert your JSON to array using json_decode. if you are converting array to JSON, then you may perform these actions prior to conversion.

  • Create an array of months.

  • use usort method to sort your array with the help of $months.
  • convert your array back to JSON using json_encode.

CODE

$json = <<<JSON
[  
   {  
      "order_id":34,
      "user_id":17,
      "sum":65000,
      "month":"May"
   },
   {  
      "order_id":32,
      "user_id":19,
      "sum":15000,
      "month":"July"
   },
   {  
      "order_id":29,
      "user_id":1,
      "sum":20000,
      "month":"April"
   },
   {  
      "order_id":29,
      "user_id":1,
      "sum":20000,
      "month":"January"
   }
]
JSON;
$arr = json_decode($json, true);
$months = [
    'January' => 1,
    'Feburary' => 2,
    'March' => 3,
    'April' => 4,
    'May' => 5,
    'June' => 6,
    'July' => 7,
    'August' => 8,
    'September' => 9,
    'October' => 10,
    'November' => 11,
    'December' => 12
];
usort($arr, function ($x, $y) use($months) {
    return $months[$x['month']] - $months[$y['month']];
});
$json = json_encode($arr);

2 Comments

Yes this is also one approch. I like the way you sorted it. I use the same usort. And helped me to resolve without any problem.
Thank you for updating it with more step by step guide. I am sure lots of people in future as well take reference from this. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.