note 25619 deleted from function.array-multisort by aidan
| From: | aidan@php.net | Date: | Sun, 03 Oct 2004 10:58:31 +0000 |
| Subject: | note 25619 deleted from function.array-multisort by aidan | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-77807@lists.php.net to get a copy of this message | ||
Note Submitter: tnguyen at scpie dot com
----
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:
<?php
$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:
<?php
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.