简体   繁体   中英

Sort an PHP JSON array by month

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)

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

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');
  • 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM