how to sort by value in associative array
| From: | Peter J. Schoenster | Date: | Sat, 06 Apr 2002 16:36:18 +0000 |
| Subject: | how to sort by value in associative array | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-91870@lists.php.net to get a copy of this message | ||
Hi,
I have the array below and I want to sort on language. Can't figure it
out. Below the array is what I tried
$ArrayOfNewsLinks = array(
"http://dailynews.yahoo.com/fc/World/Brazil/"
=> array(
title => 'Yahoo Brazil News',
category => 'news',
language => 'English',
),
"http://news.bbc.co.uk/hi/english/world/americas/default.stm"
=>
array(
title => 'BBC 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");
But then I do this:
while (list ($key, $val) = each ($ArrayOfNewsLinks)) {
$NewsLinks .= "
<p class=\"CompactLinks\">
<a href=\"$key\" title=\"$val[language] $val[title]\"
target=\"new\">$val[title]</a>
</p>
";
}
And the order is not by language.
Peter
http://www.readbrazil.com/
Answering Your Questions About Brazil