note 25619 added to function.array-multisort
| From: | tnguyen at scpie dot com | Date: | Mon, 30 Sep 2002 19:09:31 +0000 |
| Subject: | note 25619 added to function.array-multisort | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-37433@lists.php.net to get a copy of this message | ||
After a long search, I have finally found the way to a sort mulit-dimensional array by one of its
columns.
If you have the following array:
$data_array["FirstName"][0] = "John";
$data_array["FirstName"][1] = "Marta";
$data_array["FirstName"][2] = "Trent";
$data_array["LastName"][0] = "Lin";
$data_array["LastName"][1] = "Tremblay";
$data_array["LastName"][2] = "Nguyen";
$data_array["Extension"][0] = 5555;
$data_array["Extension"][1] = 2222;
$data_array["Extension"][2] = 3333;
and wanted to sort by the column, "Extension", in ascending order to output the following:
FirstName LastName Extension
Marta Tremblay 2222
Trent Nguyen 3333
John Lin 5555
use:
array_multisort($data_array["Extension"], SORT_NUMERIC, SORT_ASC,
$data_array["FirstName"],$data_array["LastName"]);
Note: If you have more columns, list them at the end.
--
http://www.php.net/manual/en/function.array-multisort.php
http://master.php.net/manage/user-notes.php?action=edit+25619
http://master.php.net/manage/user-notes.php?action=delete+25619
http://master.php.net/manage/user-notes.php?action=reject+25619