note 27058 deleted from function.strtotime by sniper
| From: | sniper@php.net | Date: | Tue, 21 Oct 2003 11:02:53 +0000 |
| Subject: | note 27058 deleted from function.strtotime by sniper | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-59013@lists.php.net to get a copy of this message | ||
Note Submitter: iban@dilemadigital.com
----
Hello! I modified the function array_qsort2 for include the order by DATES... If $model is
"date" compare strings "formated dates" of $column on array (sorry my english
:-P ). I rename function version: array_qsort3();
Example:
$array= array(
array("date"=>"2002/06/01", "name"=>"albert"),
array("date"=>"10 September 2002",
"name"=>"flanagan") );
result:
$array= array(
array("date"=>"10 September 2002",
"name"=>"flanagan"),
array("date"=>"2002/06/01", "name"=>"albert") );
Te new code is:
function array_qsort3 (&$array, $column=0, $order="SORT_ASC", $first=0, $last= -2,
$model="text")
{
// $array - the array to be sorted
// $column - index (column) on which to sort
// can be a string if using an associative array
// $order - SORT_ASC (default) for ascending or SORT_DESC for descending
// $first - start index (row) for partial array sort
// $last - stop index (row) for partial array sort
// $model - "date" for a order by date, "text" (or other string) for normal
order
if($last == -2) $last = count($array) - 1;
if($last > $first) {
$alpha = $first;
$omega = $last;
if($model=="date")
$guess = strtotime($array[$alpha][$column]);
else
$guess = $array[$alpha][$column];
while($omega >= $alpha) {
if($model=="date") {
if($order == "SORT_ASC") {
while(strtotime($array[$alpha][$column]) < $guess) $alpha++;
while(strtotime($array[$omega][$column]) > $guess) $omega--;
} else {
while(strtotime($array[$alpha][$column]) > $guess) $alpha++;
while(strtotime($array[$omega][$column]) < $guess) $omega--;
}
} else {
if($order == "SORT_ASC") {
while($array[$alpha][$column] < $guess) $alpha++;
while($array[$omega][$column] > $guess) $omega--;
} else {
while($array[$alpha][$column] > $guess) $alpha++;
while($array[$omega][$column] < $guess) $omega--;
}
}
if($alpha > $omega) break;
$temporary = $array[$alpha];
$array[$alpha++] = $array[$omega];
$array[$omega--] = $temporary;
}
array_qsort3 ($array, $column, $order, $first, $omega, $model);
array_qsort3 ($array, $column, $order, $alpha, $last, $model);
}
return $array;
}