kalpaitch
kalpaitch

Reputation: 5261

Sorting the array from an exploded string returns true instead of the sorted array

I have the following string which I am trying to explode and sort from earliest date to latest date:

$dates = '2010-11-05,2010-11-05,2010-11-06,2010-11-06,2010-11-07,2010-11-07,2010-11-08';

I've obviously tried sort(explode(',', $dates)); as well as usort, setting different sort flags, but this just returns true.

Which array sorting function am I looking for?

Upvotes: 2

Views: 488

Answers (2)

Álvaro González
Álvaro González

Reputation: 146350

<?php

$dates = '2010-11-05,2010-11-05,2010-11-06,2010-11-06,2010-11-07,2010-11-07,2010-11-08';

$array = explode(',', $dates);
sort($array);

print_r($array);

If you look carefully at the manual page for sort() you'll see that it receives its argument by reference:

bool sort ( array &$array [, int $sort_flags = SORT_REGULAR ] )

If you don't feed it with a variable, the results of the sorting will be lost since they can't be stored anywhere.

Upvotes: 7

Mchl
Mchl

Reputation: 62359

$dates = "2010-11-05,2010-11-05,2010-11-06,2010-11-06,2010-11-08,2010-11-06,2010-11-08";
$da = explode(',',$dates);
sort($da);
var_dump($da);

Upvotes: 1

Related Questions