Re: How to sort by 3rd row in an 2d-Array

From: Date: Fri, 12 Apr 2002 18:01:44 +0000
Subject: Re: How to sort by 3rd row in an 2d-Array
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-92796@lists.php.net to get a copy of this message
Or try the usort() function? <?php ... myArray[1][firstname] = "Joe"; myArray[1][lastname] = "Smith"; myArray[1][company] = "Bullock"; myArray[1][email] = "joe@bullock.com"; myArray[2][firstname] = "Jim"; myArray[2][lastname] = "Cords"; myArray[2][company] = "Jamen"; myArray[2][email] = "234@hotmail.com"; ... function companyCmp ($a, $b) { return strcmp($a["company"],$b["company"]); } usort ($myArray, "companyCmp"); ... ?> -Steve On Friday, April 12, 2002, at 12:26 PM, Kevin Stone wrote:
This is a terribly inneficient way of handling your situation but I believe it would work. // First we're going to make a list of all company values for($i=0; $i<count($myarray); $i++) {
    $company_values[] = $myarray[$i][company];
} // Then we're going to order the array using a standard function. natsort($company_values); // Then we're going to go through $myarray looking for each matching // value and build a new array based on the order of the sorted list. for($i=0; $i<count($company_values); $i++) {
    for ($j=0; $j=<count($myarray); $j++)
    {
        if ($company_values[$i] == $myarray[$j][company])
        {
            $mynewarray[] = $myarray[$j];
        }
    }
} // bada bing bada boom you gots your company sorted list... I think $myarray = $mynewarray; I wrote this off the top of my head in like three minutes so any and all corrections would be most welcome. ;) -Kevin ----- Original Message ----- From: "SED" <sed@sed.is> To: <php-general@lists.php.net> Sent: Friday, April 12, 2002 10:25 AM Subject: [PHP] How to sort by 3rd row in an 2d-Array
Hi, I'm trying to sort an array like following myArray[1][firstname] = "Joe"; myArray[1][lastname] = "Smith"; myArray[1][company] = "Bullock"; myArray[1][email] = "joe@bullock.com"; myArray[2][firstname] = "Jim"; myArray[2][lastname] = "Cords"; myArray[2][company] = "Jamen"; myArray[2][email] = "234@hotmail.com"; etc... by the company name. How can I do it? I found the solution on php.net (http://www.php.net/manual/en/function.array-multisort.php) but it sorts only the first row: foreach ($myArray as $val) {
        $sortarray[] = $val['nafn'];
echo $val['stadur']; } array_multisort($myArray, $sortarray); Thanks in advance! SED -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
-- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php


« previous php.general (#92796) next »