note 54957 modified in function.usort by danbrown

From: Date: Sun, 23 Jan 2011 16:28:40 +0000
Subject: note 54957 modified in function.usort by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-176074@lists.php.net to get a copy of this message
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; } ?> --was-- 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@fishies.com'; $test[0]['phone'] = '123-123-1234'; $test[0]['trick'] = 'mezopia'; $test[1]['name'] = 'Amanda'; $test[1]['email'] = 'hot@hotmail.com'; $test[1]['phone'] = '123-123-1235'; $test[1]['trick'] = 'youarecool'; $test[2]['name'] = 'john'; $test[2]['email'] = 'wowee@yahoo.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; } ?> http://php.net/manual/en/function.usort.php

« previous php.notes (#176074) next »