Re: sorting associative array

From: Date: Mon, 24 Jan 2005 02:14:39 +0000
Subject: Re: sorting associative array
References: 1 2 3 4  Groups: php.general 
Request: Send a blank email to php-general+get-207107@lists.php.net to get a copy of this message
Jochem Maas wrote:
Jeffery Fernandez wrote:
Kurt Yoder wrote:
Use usort (stealing from php docs): function cmp($a['score'], $b['score']) { if ($a['score'] == $b['score']) {
       return 0;
} return ($a['score'] < $b['score']) ? -1 : 1; } $data = array(...); usort($data, "cmp");
This won't work for me as I have 500+ records to sort based on the score key.. looking at jochem's class now. Thanks
which wont help much - assuming what you say is true (why wont it work? have you tried it). sort method from the class:
    function sort()
    {
       if(count($this->sortKeys)) {
           usort($this->dataArray, array($this,"_sortcmp"));
       }
    }
Thanks jochem, I had a second look at it and I figured it out. I had problem implementing it with a class but now I have figured how to use it.
cheers, Jeffery
Jeffery
On Jan 23, 2005, at 8:39 AM, Jeffery Fernandez wrote:
I have the following multi-dimentional array and I want to sort it by the "score" key value print_r($data); Array ( [0] => Array
       (
           [video_id] => 71
           [old_id] => 7854
           [title] => When the fire comes
           [video_copies] => 1
           [running_time] => 48
           [date_published] => 06/02
           [place_published] => Australia
           [abstract] => ABC TV Gives details on many aspects of bushfires: .......................
           [library_medium_id] => 5
           [library_type] => 4
           [score] => 6.3310546875
       )
[1] => Array
       (
           [video_id] => 9
           [old_id] => 7792
           [title] => Fire awareness
           [video_copies] => 1
           [running_time] => 15
           [date_published] =>            [place_published] => Victoria
           [abstract] => Safetycare Australia A general video lookin.........
           [library_medium_id] => 5
           [library_type] => 4
           [score] => 3.1997931003571
       )
) any ideas. Thanks Jeffery -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
-- Kurt Yoder http://yoderhome.com


« previous php.general (#207107) next »