按月对PHP JSON数组进行排序

时间:2022-04-17 15:55:28

i am trying to short an array by MONTH name.

我试图通过MONTH名称缩短数组。

[  
   {  
      "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.

我尝试了arsort,krsort,array_reverse(),但这些方法无法缩短它们。所以寻找其他解决方案。

Thank you! (in advance)

谢谢! (提前)

3 个解决方案

#1


3  

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

直接任何函数都不能在这里应用,因为你的数据是json格式的,你可以像下面这样实现: -

<?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

PHP重新排序月份名称数组

#2


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解码为二维数组,您可以尝试使用usort和回调函数来比较像这样的月份名称

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

#3


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.

    您需要使用json_decode将JSON转换为数组。如果要将数组转换为JSON,则可以在转换之前执行这些操作。

  • Create an array of months.

    创建一个月的数组。

  • use usort method to sort your array with the help of $months.
  • 使用usort方法在$ months的帮助下对数组进行排序。

  • convert your array back to JSON using json_encode.
  • 使用json_encode将您的数组转换回JSON。

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

#1


3  

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

直接任何函数都不能在这里应用,因为你的数据是json格式的,你可以像下面这样实现: -

<?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

PHP重新排序月份名称数组

#2


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解码为二维数组,您可以尝试使用usort和回调函数来比较像这样的月份名称

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

#3


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.

    您需要使用json_decode将JSON转换为数组。如果要将数组转换为JSON,则可以在转换之前执行这些操作。

  • Create an array of months.

    创建一个月的数组。

  • use usort method to sort your array with the help of $months.
  • 使用usort方法在$ months的帮助下对数组进行排序。

  • convert your array back to JSON using json_encode.
  • 使用json_encode将您的数组转换回JSON。

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