note 25619 modified in function.array-multisort by vrana

From: Date: Tue, 17 Aug 2004 13:59:21 +0000
Subject: note 25619 modified in function.array-multisort by vrana
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-80043@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: <?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. --was-- 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://php.net/manual/en/function.array-multisort.php

« previous php.notes (#80043) next »