Multidimensional array help
| From: | Gerard Samuel | Date: | Mon, 21 Oct 2002 17:00:09 +0000 |
| Subject: | Multidimensional array help | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-120768@lists.php.net to get a copy of this message | ||
Hi all. Im trying to sort a multidimensional array using uasort().
I haven't used it before, so Im not too sure on its usage.
Here is what I have ->
--------------------
<?php
echo header('Content-type: text/plain');
$foo[0][] = array('title' => 'Title 1', 'summary' => 'Summary 1', 'rank' => 13);
$foo[1][] = array('title' => 'Title 2', 'summary' => 'Summary 2', 'rank' => 7);
$foo[1][] = array('title' => 'Title 3', 'summary' => 'Summary 3', 'rank' => 3);
$foo[2][] = array('title' => 'Title 4', 'summary' => 'Summary 4', 'rank' => 11);
$foo[1][] = array('title' => 'Title 9', 'summary' => 'Summary 9', 'rank' => 4);
function cmp ($a, $b)
{
foreach($a as $k => $null)
{
if ($a[$k]['rank'] < $b[$k]['rank']) return 1;
if ($a[$k]['rank'] > $b[$k]['rank']) return -1;
if ($a[$k]['rank'] == $b[$k]['rank']) return 0;
}
}
uasort ($foo, 'cmp');
print_r($foo);
?>
----------------------
Im trying to sort the array in descending order by the rank's value.
Im not sure if Im heading in the right direction with this, but any help would be appreciated.
Thanks.
--
Gerard Samuel
http://www.trini0.org:81/
http://dev.trini0.org:81/