note 47294 added to function.array-multisort
| From: | jagallegos at gmail dot com | Date: | Wed, 10 Nov 2004 12:27:40 +0000 |
| Subject: | note 47294 added to function.array-multisort | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-80342@lists.php.net to get a copy of this message | ||
Hi! This is my source code to sort datatables
<?php
$table[0]["index"] = 1;
$table[0]["name"] = "d";
$table[1]["index"] = 4;
$table[1]["name"] = "a";
$table[2]["index"] = 3;
$table[2]["name"] = "b";
$table[3]["index"] = 2;
$table[3]["name"] = "c";
$table_sorted = sort_table($table,"name",0);
print_r($table);
print "\n\n";
print_r($table_sorted);
/*
* SORTING DATATABLES BY A SPECIFIED FIELD OR COLUMN IN ASC-DESC MODE
* MODE = 0 (ASC) 1 (DESC)
*/
function sort_table($datatable,$field,$mode) {
$i_cnt = count($datatable);
for($i=0;$i<$i_cnt;$i++) {
foreach($datatable[$i] as $key => $value) {
$array[$key][$i] = $value;
}
}
$mode == 0 ? asort($array[$field]) : arsort($array[$field]);
$_keys = array_keys($array[$field]);
for($i=0;$i<$i_cnt;$i++) {
foreach($array as $key => $value) {
$_hash[$i][$key] = $array[$key][$_keys[$i]];
}
}
return $_hash;
}
?>
----
Manual Page -- http://www.php.net/manual/en/function.array-multisort.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+47294
Delete -- http://master.php.net/manage/user-notes.php?action=delete+47294&report=yes
Reason: bad code -- http://master.php.net/manage/user-notes.php?action=delete+47294&report=yes&reason=bad+code
Reason: spam -- http://master.php.net/manage/user-notes.php?action=delete+47294&report=yes&reason=spam
Reason: useless example -- http://master.php.net/manage/user-notes.php?action=delete+47294&report=yes&reason=useless+example
Reason: contains commercial links -- http://master.php.net/manage/user-notes.php?action=delete+47294&report=yes&reason=contains+commercial+links
Reason: useless note -- http://master.php.net/manage/user-notes.php?action=delete+47294&report=yes&reason=useless+note
Reject -- http://master.php.net/manage/user-notes.php?action=reject+47294&report=yes
Search -- http://master.php.net/manage/user-notes.php