note 25878 modified in function.array-multisort by vrana

From: Date: Tue, 17 Aug 2004 13:59:18 +0000
Subject: note 25878 modified in function.array-multisort by vrana
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-79946@lists.php.net to get a copy of this message
I had trouble with the above examples using this function to sort a MYSQL array and found that usort was a better choice. The below example sorts a text column which may contain a date using the strtotime function. <?php function sortbyorderdate($a, $b) { if (empty($a["order_date"])) return -1; if (empty($b["order_date"])) return 1; return strcmp(strtotime($a["order_date"]), strtotime($b["order_date"])); } while ($rows[] = mysql_fetch_array($result)) {} usort($rows, "sortbyorderdate"); ?> --was-- I had trouble with the above examples using this function to sort a MYSQL array and found that usort was a better choice. The below example sorts a text column which may contain a date using the strtotime function. function sortbyorderdate($a, $b) { if (empty($a["order_date"])) return -1; if (empty($b["order_date"])) return 1; return strcmp(strtotime($a["order_date"]), strtotime($b["order_date"])); } while ($rows[] = mysql_fetch_array($result)) {} usort($rows, "sortbyorderdate"); http://php.net/manual/en/function.array-multisort.php

« previous php.notes (#79946) next »