Sorts multiple arrays based on one array.
The array_multisort() function orders the elements in multiple arrays by the values of the first array passed to the function. This is similar to each array being a column in a table sorted by the first column. This function is very flexible with how arguments are passed to it. The first argument has to be an array. After that, ordering and evaluation flags can optionally be specified (either one can come first). Additional arrays with order and evaluation flags can optionally follow. If the array was sorted successfully, TRUE is returned.
<?php
$countries = array("Spain", "United Kingdom", "United States", "Sweden", "Italy", "Italy", "United States");
$cities = array("Madrid", "London", "New York", "Stockholm", "Venice", "Rome", "Chicago");
$languages = array("Spanish", "English", "English", "Swedish", "Italian", "Italian", "English");
array_multisort($countries, SORT_ASC, SORT_STRING,
$cities, SORT_ASC, $languages);
foreach ($countries as $country) {
print "$country; ";
}
print "<br>";
foreach ($cities as $city) {
print "$city; ";
}
print "<br>";
foreach ($languages as $language) {
print "$language; ";
}
?>
Italy; Italy; Spain; Sweden; United Kingdom; United States; United States;
Rome; Venice; Madrid; Stockholm; London; Chicago; New York;
Italian; Italian; Spanish; Swedish; English; English; English;
A few arrays hold a "table" where each "row" has a country, a city in that country, and a language spoken in that city. The elements are sorted based on country first, then city, and then language. The result is printed one column per line.