9

I have a non-associative array where the data that comes in is not sorted (I'm receiving the data from an outside system and cannot force it to come into the array in sorted order.) Is there any way to sort the values? I've tried this:

$wedTrackTimes = array("9:30 AM-10:30 AM", "8:15 AM-9:15 AM", "12:30 PM-1:30 PM", "2:00 PM-3:00 PM", "3:30 PM-4:30 PM");
$wedTrackTimes = array_unique($wedTrackTimes);
$wedTrackTimes = sort($wedTrackTimes);
print_r($wedTrackTimes);

But instead of returning a sorted array, it returns 1. I'm assuming it's because it's non-associative, so there are no keys. Is there any way to sort an array by value only? We really need the 9:30 AM time slot to fall after the 8:15 AM slot, as it should.

UPDATE

Thanks to all for the answers; that did make the array sort, but not as expected. If I use the default sort type, I get this:

Array
(
    [0] => 12:30 PM-1:30 PM
    [1] => 2:00 PM-3:00 PM
    [2] => 3:30 PM-4:30 PM
    [3] => 8:15 AM-9:15 AM
    [4] => 9:30 AM-10:30 AM
)

Using SORT_NUMERIC I get this:

Array
(
    [0] => 2:00 PM-3:00 PM
    [1] => 3:30 PM-4:30 PM
    [2] => 8:15 AM-9:15 AM
    [3] => 9:30 AM-10:30 AM
    [4] => 12:30 PM-1:30 PM
)

Using SORT_STRING I get this:

Array
(
    [0] => 12:30 PM-1:30 PM
    [1] => 2:00 PM-3:00 PM
    [2] => 3:30 PM-4:30 PM
    [3] => 8:15 AM-9:15 AM
    [4] => 9:30 AM-10:30 AM
)

What I need is:

Array
(
    [0] => 8:15 AM-9:15 AM
    [1] => 9:30 AM-10:30 AM
    [2] => 12:30 PM-1:30 PM
    [3] => 2:00 PM-3:00 PM
    [4] => 3:30 PM-4:30 PM


)

Is this possible?

8 Answers 8

21

Sort works by reference (that means it sorts whatever you pass to it), it returns true/false based on failure. What you're doing here:

$wedTrackTimes = sort($wedTrackTimes);

is assigning the value $wedTrackTimes to TRUE or FALSE.

Try

sort($wedTrackTimes);
print_r($wedTrackTimes);
Sign up to request clarification or add additional context in comments.

8 Comments

Whoops, not finished yet. It did work, in the sense that it sorted, but did not sort as expected. See OP for details...
You look like you need a custom function.
The question was updated 17 minutes after posting. It was then clarified that a standard sort was not sufficient. A chronological sort was required. This answer does not provide the desired sorting outcome.
@mickmackusa That might be true, but (as with Git) editing after the fact often leads to unpredictable results.
The coding intent and sample data didn't change. The first hurdle was the "printing 1" issue, but the goal was always to sort chronologically. The OP just didn't yet realize what they didn't understand.
|
5

So, it looks like you're looking for something a little more advanced than a standard sort.

// WARNING: THIS IS *NOT* BY REFERENCE. IT RETURNS A NEW ARRAY.
function getSortedTimes(array $group)
{
    $tmp = array();
    foreach( $group as $times )
    {
        // Basically, I am pairing the string for the start time with 
        // a numeric value.
        $tmp[$times] = strtotime(substr($times, 0, strpos($times, '-')));
    }
    // asort is like sort, but it keeps the pairings just created.
    asort($tmp);
    // the keys of $tmp now refer to your original times.
    return array_keys($tmp);
}

Comments

5

That's right, sort returns bool. Just use that:

sort($wedTrackTimes);

Comments

4

sort, like all of php's sorting functions, sorts in-place. It returns true if the sorting was successful, false otherwise. This result is irrelevant if you're only sorting strings/numbers.

$wedTrackTimes = array("9:30 AM-10:30 AM", "8:15 AM-9:15 AM",
                       "12:30 PM-1:30 PM", "2:00 PM-3:00 PM", "3:30 PM-4:30 PM");
$wedTrackTimes = array_unique($wedTrackTimes);
sort($wedTrackTimes);
print_r($wedTrackTimes);

is the way to go.

Comments

1

change your code to this:

$wedTrackTimes = array_unique($wedTrackTimes);
sort($wedTrackTimes);
print_r($wedTrackTimes);

as you can read in the documentation, the return value of sort() is true/1 or false/0 and indicates if sorting was possible or an error occured.

Comments

1

Remove $wedTrackTimes = before sort.

 $wedTrackTimes = array("9:30 AM-10:30 AM", "8:15 AM-9:15 AM", "12:30 PM-1:30 PM", "2:00 PM-3:00 PM", "3:30 PM-4:30 PM");
 $wedTrackTimes = array_unique($wedTrackTimes);
 sort($wedTrackTimes);
 print_r($wedTrackTimes);

Comments

0

In your code

$wedTrackTimes = array("9:30 AM-10:30 AM", "8:15 AM-9:15 AM", "12:30 PM-1:30 PM", "2:00 PM-3:00 PM", "3:30 PM-4:30 PM");
$wedTrackTimes = array_unique($wedTrackTimes);
**$wedTrackTimes = sort($wedTrackTimes);**
print_r($wedTrackTimes);

Don't assign variable to sort function, Basically what you are doing is assigning sort's value (which will be true or false) into wedTrackTimes, Instead of this use

$wedTrackTimes = array("9:30 AM-10:30 AM", "8:15 AM-9:15 AM", "12:30 PM-1:30 PM", "2:00 PM-3:00 PM", "3:30 PM-4:30 PM");
    $wedTrackTimes = array_unique($wedTrackTimes);
    **sort($wedTrackTimes);**
    print_r($wedTrackTimes);

Comments

0

Sensibly sorting on time (chronologically) will call for strtotime().

Here is a one-liner for you using array_multisort(). array_map() and strtotime() are called to generate an array to be used purely for the sorting order.

array_multisort(array_map(function($v){return strtotime(strstr($v,'-',true));},$wedTrackTimes),$wedTrackTimes);

For anyone that is lost in that syntax, here is the same functionality on more lines.

Code: (Demo)

$wedTrackTimes=[
    "9:30 AM-10:30 AM",
    "8:15 AM-9:15 AM",
    "12:30 PM-1:30 PM",
    "2:00 PM-3:00 PM",
    "3:30 PM-4:30 PM"
];

foreach($wedTrackTimes as $time){  // iterate the time strings
    $timestamps[]=strtotime(strstr($time,'-',true));  // store the first time of the time range as a unix timestamp
}
array_multisort($timestamps,$wedTrackTimes);  // use $timestamps to sort $wedTrackTimes

var_export($wedTrackTimes);

Output:

array (
  0 => '8:15 AM-9:15 AM',
  1 => '9:30 AM-10:30 AM',
  2 => '12:30 PM-1:30 PM',
  3 => '2:00 PM-3:00 PM',
  4 => '3:30 PM-4:30 PM',
)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.