PHP Re-Order Array of Month Names

PHP re-order array of month names

Convert the months to a numerical value. Then order your array numerically using sort()

You can use this question: convert month from name to number

@Matthew's answer seems to work well:

$date = date_parse('July');;
echo $date["month"];

Working solution

$months = array("April", "August", "February");

usort($months, "compare_months");
var_dump($months);

function compare_months($a, $b) {
$monthA = date_parse($a);
$monthB = date_parse($b);

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

PHP re-order array of month names(associative array)

Make order array and then sort by usort functon

$seq = array_flip([ 'August', 'September', 'October',  'November' ]);

usort($arr, function ($i1, $i2) use($seq) { return $seq[$i1['label']] - $seq[$i2['label']]; });

demo

Sort array with month name and year in php

You can use usort and use strtotime to convert the string to time.

$ar = ['March 2018',
'March 2018',
'November 2017',
'December 2017',
'January 2018',
'January 2018',
'February 2018'
];

usort( $ar , function($a, $b){
$a = strtotime($a);
$b = strtotime($b);
return $a - $b;
});

echo "<pre>";
print_r( $ar );
echo "</pre>";

This will result to:

Array
(
[0] => November 2017
[1] => December 2017
[2] => January 2018
[3] => January 2018
[4] => February 2018
[5] => March 2018
[6] => March 2018
)

How to sort a multidimensional array by month names in php

An approach, using usort() and the appropriate comparison function is an option:

<?php
$array = array(
array("netDe" => 22970.00, "netEran" => 71330.00, "compiled_month" => "May/2021"),
array("netDe" => 22970.00, "netEran" => 71330.00, "compiled_month" => "Jun/2021"),
array("netDe" => 65970.00, "netEran" => 28330.00, "compiled_month" => "Jul/2021"),
array("netDe" => 10396.00, "netEran" => 27324.00, "compiled_month" => "Jan/2021"),
array("netDe" => 20792.00, "netEran" => 54648.00, "compiled_month" => "Feb/2021" )
);

usort(
$array,
function ($a, $b) {
return
DateTime::createFromFormat('d/M/Y', "01/".$a["compiled_month"]) <=>
DateTime::createFromFormat('d/M/Y', "01/".$b["compiled_month"]);
}
);
?>

How to sort array by month abbreviation

You was close.

The month alias map (Jan => 1, ...) was a good idea.

You just needed to loop though all arrays in the main array

and sort them (by reference).

Note: im using uasort, but you could also use usort.

The source array:

$shipping_chart_month = [
[
"name" => "8=>00 AM",
"data" => [
[
"x" => "May",
"y" => 37
],
[
"x" => "Nov",
"y" => 32
],
[
"x" => "Apr",
"y" => 1
],
[
"x" => "Aug",
"y" => 45
],
[
"x" => "Sep",
"y" => 19
],
[
"x" => "Jul",
"y" => 13
],
[
"x" => "Oct",
"y" => 43
],
[
"x" => "Jun",
"y" => 31
],
[
"x" => "Feb",
"y" => 0
],
[
"x" => "Jan",
"y" => 0
],
[
"x" => "Mar",
"y" => 0
]
]
],
[
"name" => "9=>00 AM",
"data" => [
[
"x" => "Apr",
"y" => 26
],
[
"x" => "Oct",
"y" => 84
],
[
"x" => "Sep",
"y" => 35
],
[
"x" => "Jul",
"y" => 26
],
[
"x" => "Feb",
"y" => 6
],
[
"x" => "Nov",
"y" => 96
],
[
"x" => "Mar",
"y" => 10
],
[
"x" => "May",
"y" => 50
],
[
"x" => "Aug",
"y" => 66
],
[
"x" => "Jun",
"y" => 36
],
[
"x" => "Jan",
"y" => 0
]
]
]
];

The sorting:

// The month alias map.
$monthAliasMap = array(
// {month alias} => {sort priority (lower before higher)}
'Jan' => 1,
'Feb' => 2,
'Mar' => 3,
'Apr' => 4,
'May' => 5,
'Jun' => 6,
'Jul' => 7,
'Aug' => 8,
'Sep' => 9,
'Oct' => 10,
'Nov' => 11,
'Dec' => 12,
);
// Note: using each $array in $shipping_chart_month by reference.
foreach ($shipping_chart_month as &$array) {
uasort($array['data'], function ($a, $b) use ($monthAliasMap) {
// $a | $b example:
// array (
// 'x' => 'May',
// 'y' => 37,
// )

// Set the offset we expect the month alias on.
$offset = 'x';
// Get the month alias from a and b.
$aMonthAlias = $a[$offset]; // F.e. "Jan" or "Dec" ...
$bMonthAlias = $b[$offset]; // F.e. "Jan" or "Dec" ...
// Get (map) the sort priority for a and b.
$aPriority = (int)$monthAliasMap[$aMonthAlias]; // F.e. 1 or 2 ...
$bPriority = (int)$monthAliasMap[$bMonthAlias]; // F.e. 1 or 2 ...

// Sort them.
if ($aPriority === $bPriority) {
return 0;
}
// a < b === asc ; a > b === desc
return ($aPriority < $bPriority) ? -1 : 1;
});

}
unset($array); // Release reference.

Result (shortened):

[
0 => [
'name' => '8=>00 AM',
'data' => [
9 => [
'x' => 'Jan',
'y' => 0,
],
8 => [
'x' => 'Feb',
'y' => 0,
],
// ...
1 => [
'x' => 'Nov',
'y' => 32,
],
],
],
1 => [
'name' => '9=>00 AM',
'data' => [
10 => [
'x' => 'Jan',
'y' => 0,
],
4 => [
'x' => 'Feb',
'y' => 6,
],
// ...
5 => [
'x' => 'Nov',
'y' => 96,
],
],
],
];

PHP - sort array by month and year

uksort is the right function to use :

uksort($ar, function($a1, $a2) {
$time1 = strtotime($a1);
$time2 = strtotime($a2);

return $time1 - $time2;
});

print_r($ar);

Sort array by month based on value

You will need to define a custom function for sorting your array

function sortByDate($a, $b) {
return strtotime($a) - strtotime($b);
}

usort($testar, 'sortByDate');

I have derived the answer from this one

Sort an PHP JSON array by month

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

sorting arrays by months

1.) you need to use uksort as you're sorting keys

2.) you should compare uppercase-keys in order to match your month-array:

$a = strtoupper($a);
$b = strtoupper($b);


Related Topics



Leave a reply



Submit