I have a 2D array named $props, the structure is as follows,
$props = [
['name' => 'Mathmatics', 'time' => '03:01:PM - 04:50:PM'],
['name' => 'History', 'time' => '11:30:AM - 01:30:PM'],
['name' => 'French', 'time' => '01:31:PM - 03:00:PM'],
];
I need to sort the array by ‘time’ key, to get the following result:
[
['name' => 'History', 'time' => '11:30:AM - 01:30:PM'],
['name' => 'French', 'time' => '01:31:PM - 03:00:PM'],
['name' => 'Mathmatics', 'time' => '03:01:PM - 04:50:PM'],
];
I have found a solution with usort, the solution is as follows:
usort($props, function ($a, $b) {
return $a["time"] - $b["time"];
});
However, this is not working maybe because of special format of my time (but I will have to follow this specific time format.) and shows an error and do nothing to the array. The error:
Notice: A non well formed numeric value encountered in C:xampp…..
3
Answers
given the format of the date (
03:01:PM - 04:50:PM
etc.) it needs to be made sort able, while keeping the original value.from your question it could be seen that only the first part (
03:01:PM
) is in use for sorting. even if not the case, lets keep it for the example (it can be easily extended).given the C locale, transforming the time allows to get a string that can be just sorted (binary string order):
given a single
$time
as input, the transformation can be done with a regular expression search and replace:Now to actually sort the
$props
array, sort the array of all transformed$times
and$props
:Now
$props
is sorted according to$times
:Example on 3v4l.org.
This solution uses
usort
with a special sorting function. Date objects are created usingsubstr
andDateTime::createFromFormat
. The spaceship operator is use for comparison.Try self: https://3v4l.org/Fq9U5
This variant sorts by the start of the time interval. If only the end is required you can use
For an approach with no regex and fewest iterated function calls, use
array_map()
with iterated calls ofcreateFromFormat()
, and use that generated sorting array as the first parameter ofarray_multisort()
to sort the input array.Note that
+
in thecreateFromFormat()
method demands that all remaining characters are ignored.Code: (Demo)