note 94483 deleted from function.sort by danbrown
| From: | danbrown@php.net | Date: | Tue, 17 Nov 2009 15:25:19 +0000 |
| Subject: | note 94483 deleted from function.sort by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-162980@lists.php.net to get a copy of this message | ||
Note Submitter: toenie
----
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