note 94483 added to function.sort
| From: | toenie at osu1 dot php dot net | Date: | Sat, 07 Nov 2009 22:04:40 +0000 |
| Subject: | note 94483 added to function.sort | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-162621@lists.php.net to get a copy of this message | ||
If you want to sort while using an 2 dimensional array, you can use this script I just wrote for
myself. I thought it could be helpful for other people too.
<?php
function order_array ($array, $key) {
$tmp = array();
foreach($array as $akey => $array2) {
$tmp[$akey] = $array2[$key];
}
sort($tmp , SORT_NUMERIC ); //Change this if you have an associative array
$tmp2 = array();
$tmp_size = count($tmp);
foreach($tmp as $key => $value) {
$tmp2[$key] = $array[$key];
}
return $tmp2;
}
?>
You can use this if you have an array similar to this one:
Array
(
[0] => Array
(
[id] => 1
[title] => Bananas rule
[last_edit] => 71
)
[1] => Array
(
[id] => 2
[title] => I'd like to have apples
[last_edit] => 40
)
[2] => Array
(
[id] => 3
[title] => People do like oranges the most
[last_edit] => 63
)
)
if you want to sort this numerical on last edit, use the function this way, where $array is the
example above:
$array = array_sort( $array , 'last_edit' );
Now it's sorted in the new way, with the keys in this given order:
1, 2, 0
----
Server IP: 83.137.20.135
Probable Submitter: 84.87.82.149
----
Manual Page -- http://www.php.net/manual/en/function.sort.php
Edit -- https://master.php.net/note/edit/94483
Del: integrated -- https://master.php.net/note/delete/94483/integrated
Del: useless -- https://master.php.net/note/delete/94483/useless
Del: bad code -- https://master.php.net/note/delete/94483/bad+code
Del: spam -- https://master.php.net/note/delete/94483/spam
Del: non-english -- https://master.php.net/note/delete/94483/non-english
Del: in docs -- https://master.php.net/note/delete/94483/in+docs
Del: other reasons-- https://master.php.net/note/delete/94483
Reject -- https://master.php.net/note/reject/94483
Search -- https://master.php.net/manage/user-notes.php