0

I have two array, first is month array second is result array,

$monthArray = array("Jan 2018,Feb 2018","Mar 2018");

Need to sort the following array in the order of months

$result =
array(
        array(
            "day" => "Feb 2018",
            "value" => "101"
        ),
        array(
           "day" => "Jan 2018",
            "value" => "18"
        ),
        array(
           "day" => "Mar 2018",
            "value" => "0"
        )

I have to sort result array in month order like below output,

Array
(
[0] => Array
    (
        [day] => Jan 2018
        [value] => 18
    )
[1] => Array
    (
        [day] => Feb 2018
        [value] => 101
    )
[2] => Array
    (
        [day] => Mar 2018
        [value] => 0
    )
)

I am trying by using sort,usort,ksort php functions but its not working.

vrock
  • 39
  • 1
  • 6
  • Do you mean `$monthArray = array("Jan 2018","Feb 2018","Mar 2018"); `? – Eddie Mar 20 '18 at 07:24
  • yes,I am trying to sort by this static month array. – vrock Mar 20 '18 at 07:26
  • @vrock can this month array be in a non-chronological order. I am assuming it can as there'd be no reason to have this array if not. – David Barker Mar 20 '18 at 07:39
  • @vrock I would like to echo DavidBarker's concern. Please confirm that the sorting array of month-years will **always** or **not always** be in ASC order. – mickmackusa Mar 20 '18 at 14:22

2 Answers2

0

Try this,

$result =
    [
    [
        "day"   => "Feb 2018",
        "value" => "101",
    ],
    [
        "day"   => "Jan 2018",
        "value" => "18",
    ],
    [
        "day"   => "Mar 2018",
        "value" => "0",
    ],
];
usort($result, function($a, $b) {
  return new DateTime($a['day']) <=> new DateTime($b['day']);
});

usort — Sort an array by values using a user-defined comparison function

Here is working demo.

Ref link.

Rahul
  • 18,271
  • 7
  • 41
  • 60
  • This would work to sort by the value of the `day` key in the result array. It would not order the results by the order defined in the month array if it is not in chronological order. – David Barker Mar 20 '18 at 07:35
0

If you want to sort chronologically, you don't need your $monthArray variable. You can use strtotime() to generate a timestamp from the month-year values and sort on these timestamps ascending. You can compare the values in the sort call by using the spaceship operator (<=>) or if your php version is older, you can use subtraction.

Code: (Demo)

$result =[
    ["day" => "Feb 2018", "value" => "101"],
    ["day" => "Jan 2018", "value" => "18"],
    ["day" => "Mar 2018", "value" => "0"]
];

usort($result,function($a,$b){return strtotime($a['day'])-strtotime($b['day']);});

var_export($result);

Output:

array (
  0 => 
  array (
    'day' => 'Jan 2018',
    'value' => '18',
  ),
  1 => 
  array (
    'day' => 'Feb 2018',
    'value' => '101',
  ),
  2 => 
  array (
    'day' => 'Mar 2018',
    'value' => '0',
  ),
)

If your sorting month-year variable can contain non-chronological ordering, then you can use array_replace().

  1. Flip $monthArray so that the values become keys.
  2. Call array_column() with a null 2nd parameter so that the array is assigned keys using the column data dictated by the 3rd parameter.
  3. Replace $monthArray values based on associative keys.
  4. Reindex the array with array_values().

Different Code, Same Result:

$monthArray = ["Jan 2018","Feb 2018","Mar 2018"];

$result =[
    ["day" => "Feb 2018", "value" => "101"],
    ["day" => "Jan 2018", "value" => "18"],
    ["day" => "Mar 2018", "value" => "0"]
];

$result = array_values(array_replace(array_flip($monthArray),array_column($result,null,'day')));
var_export($result);

Depending on the size of your input data, this may or may not be faster than usort() with two array_search() calls on each iteration. Either way the speed difference will be unnoticeable to your end users.

mickmackusa
  • 43,625
  • 12
  • 83
  • 136