note 15954 modified in function.array-multisort by vrana

From: Date: Tue, 17 Aug 2004 14:46:54 +0000
Subject: note 15954 modified in function.array-multisort by vrana
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-79955@lists.php.net to get a copy of this message
If you have an array that contains output from a SQL SELECT statment and you want to sort the array on a column, you have to create a separate array containing the values of that column like so: <?php //Get table output into $rows array $result = mysql_query("SELECT field1, field2 from table"); while ($rows[] = mysql_fetch_array($result)) {} //Sort on field2 foreach ($rows as $val) { $sortarray[] = $val['field2']; } array_multisort($rows, $sortarray); //Now, $rows is sorted on field2 ?> If anyone knows an easier way to do this, please let us know! --was-- If you have an array that contains output from a SQL SELECT statment and you want to sort the array on a column, you have to create a separate array containing the values of that column like so: //Get table output into $rows array $result = mysql_query("SELECT field1, field2 from table"); while ($rows[] = mysql_fetch_array($result)) {} //Sort on field2 foreach ($rows as $val) { $sortarray[] = $val['field2']; } array_multisort($rows, $sortarray); //Now, $rows is sorted on field2 If anyone knows an easier way to do this, please let us know! http://php.net/manual/en/function.array-multisort.php

« previous php.notes (#79955) next »