note 54957 deleted from function.usort by crell
| From: | crell@php.net | Date: | Wed, 30 Mar 2022 22:15:34 +0000 |
| Subject: | note 54957 deleted from function.usort by crell | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-217817@lists.php.net to get a copy of this message | ||
Note Submitter: Jeremy Swinborne
----
When I query a DB I usually put my record set inside of a multi-dimentional array. I finally wrote
a program that will allow you to sort your record set by column after you put it in an array.
<?php
$test = array();
$test[0]['name'] = 'jeremy';
$test[0]['email'] = 'lala@example.com';
$test[0]['phone'] = '123-123-1234';
$test[0]['trick'] = 'mezopia';
$test[1]['name'] = 'Amanda';
$test[1]['email'] = 'hot@example.com';
$test[1]['phone'] = '123-123-1235';
$test[1]['trick'] = 'youarecool';
$test[2]['name'] = 'john';
$test[2]['email'] = 'wowee@example.com';
$test[2]['phone'] = '123-123-3333';
$test[2]['trick'] = 'goneinanhour';
print_r(columnSort($test, 'name'));
function columnSort($unsorted, $column) {
$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;
}
}
return $sorted;
}
?>