note 15030 added to function.usort.php

From: Date: Fri, 24 Aug 2001 22:58:32 +0000
Subject: note 15030 added to function.usort.php
Groups: php.notes 
Request: Send a blank email to php-notes+get-17099@lists.php.net to get a copy of this message
This lets you sort an associative multi-dimensional array by multiple key/field names. Much similiar to the SQL clause ORDER BY. Enjoy. function cmp ($a, $b) { // Populate this array with your values... // Below is the SQL equivalent of // select * from blah ORDER BY date desc, type asc, name asc $vals = array( 'date' => 'd', 'type' => 'a', 'name' => 'a' ); while(list($key, $val) = each($vals)) { if($val == "d") { if ($a["$key"] > $b["$key"]) { return -1; } if ($a["$key"] < $b["$key"]) { return 1; } } if($val == "a") { if ($a["$key"] < $b["$key"]) { return -1; } if($a["$key"] > $b["$key"]) { return 1; } } } } http://www.php.net/manual/en/function.usort.php

« previous php.notes (#17099) next »