note 111033 deleted from function.usort by danbrown
| From: | danbrown@php.net | Date: | Thu, 17 Jan 2013 15:20:00 +0000 |
| Subject: | note 111033 deleted from function.usort by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-193134@lists.php.net to get a copy of this message | ||
Note Submitter: Jeremy Swinbornes 1.1
----
a little extra to Jeremy Swinbornes function:
<?php
function orderBy ($unsorted, $column, $adesc = 'ASC') {
$sorted = $unsorted;
for ($i=0; $i < sizeof($sorted)-1; $i++) {
for ($j=0; $j<sizeof($sorted)-1-$i; $j++) {
if ($sorted[$j][$column] > $sorted[$j+1][$column]) {
$tmp = $sorted[$j];
$sorted[$j] = $sorted[$j+1];
$sorted[$j+1] = $tmp;
};
};
};
if ($adesc == 'DESC') {
return array_reverse($sorted);
} else {
return $sorted;
};
};
// example:
$array = array(
array(
"searchResult" = array("example1",
"example1", "example1", "example1"),
"count" = 4,
"page" = "www.example.com/?page=1",
),
array(
"searchResult" = array("example2",
"example2"),
"count" = 2,
"page" = "www.example.com/?page=2",
),
);
// ASC
$array1 = orderBy($array, "count");
// DESC
$array2 = orderBy($array, "count", "DESC");
?>
I simply added the array_reverse() and that's it! Now you can add how to order your Array
PS: a big thanks to Jeremy Swinbornes!