note 111033 added to function.usort
| From: | JeremySwinbornes1.1 | Date: | Tue, 08 Jan 2013 16:06:04 +0000 |
| Subject: | note 111033 added to function.usort | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-192841@lists.php.net to get a copy of this message | ||
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!
----
Server IP: 69.147.83.199
Probable Submitter: 84.134.117.106
----
Manual Page -- http://php.net/manual/en/function.usort.php
Edit -- https://master.php.net/note/edit/111033
Del: integrated -- https://master.php.net/note/delete/111033/integrated
Del: useless -- https://master.php.net/note/delete/111033/useless
Del: bad code -- https://master.php.net/note/delete/111033/bad+code
Del: spam -- https://master.php.net/note/delete/111033/spam
Del: non-english -- https://master.php.net/note/delete/111033/non-english
Del: in docs -- https://master.php.net/note/delete/111033/in+docs
Del: other reasons-- https://master.php.net/note/delete/111033
Reject -- https://master.php.net/note/reject/111033
Search -- https://master.php.net/manage/user-notes.php