Re: how to sort by value in associative array

From: Date: Sat, 06 Apr 2002 22:38:02 +0000
Subject: Re: how to sort by value in associative array
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-91890@lists.php.net to get a copy of this message
On Sat, 6 Apr 2002 10:36:18 -0600, Peter J. Schoenster <peter@schoenster.com> wrote: > $ArrayOfNewsLinks = array( > "http://dailynews.yahoo.com/fc/World/Brazil/" => > array( > title => 'Yahoo Brazil News', > category => 'news', > language => 'English', > > ), ... > function cmp ($a, $b) { > if ($a[2] == $b[2]) return 0; > //return strcmp ( $a[2], $b[2]); // thought this would work > return ($a[1]>$b[1])?1:-1; > } > > > uksort ($ArrayOfNewsLinks, "cmp"); Try changing those subscripts to keys: if ($a["language"] == $b["language"]) etc. You should have a ton of PHP warnings generated from the code above, as numeric elements won't exist. As a side note, an interesting addition would be using the title as a second sort key: if ($a["language"] == $b["language"]) { return strnatcasecmp($a["title"], $b["title"]); }

« previous php.general (#91890) next »