On Thu, 2002-02-14 at 08:26, Fifield, Mike wrote:
> What I am trying to do is sort a array of arrays but I want to sort by one
> of the pieces of data stored in the arrays inside the array. For example;
> $data[blue] = array("name", "age", "time", "3");
> $data[green] = array("name", "age", "time", "7");
> $data[red] = array("name", "age", "time", "6");
> $data[yellow] = array("name", "age", "time", "2");
> $data[white] = array("name", "age", "time", "1");
> $data[black] = array("name", "age", "time", "9");
> 
> I want to be able to sort this array of arrays by the very last element, the
> numbers. 

<?php
error_reporting(E_ALL);

$data = array();
$data['blue'] = array("name", "age", "time", "3");
$data['green'] = array("name", "age", "time", "7");
$data['red'] = array("name", "age", "time", "6");
$data['yellow'] = array("name", "age", "time", "2");
$data['white'] = array("name", "age", "time", "1");
$data['black'] = array("name", "age", "time", "9");
$data['spooge'] = array();

function cmp($a, $b) {
    /* Bounds checking. To put any empty arrays at the 
     * end instead of the beginning, swap -1 and 1 in the
     * next two lines. */
    if (empty($a) && !empty($b)) return -1;
    if (empty($b) && !empty($a)) return 1;
    if (empty($a) && empty($b)) return 0;
    return (int) ($a[count($a) - 1] - $b[count($b) - 1]);
}

uasort($data, 'cmp');

print_r($data);
?>


Hope this helps,

Torben

> Mike Fifield
> Charles Schwab & Co, Inc.
> WARNING: All e-mail sent to or from this address will be received by the
> Charles Schwab corporate e-mail system and is subject to archival and review
> by someone other than the recipient.

-- 
 Torben Wilson <[EMAIL PROTECTED]>
 http://www.thebuttlesschaps.com
 http://www.hybrid17.com
 http://www.inflatableeye.com
 +1.604.709.0506


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to