Hey guys,
I've seem to come across a problem that exhausts my PHP skills (admittedly limited to begin with). I have a multi-dimensional array that needs to be sorted on three levels. Below is the array:
PHP Code:$entries = array( array("China", "Bejing", "China Exchange Program"),
array("India", "New Delhi", "India Exchange Program"),
array("United Kingdom", "London", "UK Exchange Program"),
array("United States", "New York", "NYC X Exchange Program"),
array("United States", "New York", "NYC Q Exchange Program"),
array("United States", "New York", "NYC A Exchange Program"),
array("United States", "Philadelphia", "Philadelphia Exchange Program"),
array("Thailand", "Bangkok", "Thailand Exchange Program"),
array("Germany", "Berlin", "Berlin Exchange Program"),
array("Germany", "Hamburg", "Hamburg Exchange Program"),
array("Australia", "Sydney", "Australia Exchange Program"),
array("Peru", "Lima", "Peru Exchange Program"),
array("Brazil", "Brasilia", "Brasilia Exchange Program"),
array("Brazil", "Rio de Janeiro", "Rio Exchange Program"),
array("Brazil", "Sao Paulo", "Sao Paulo Exchange Program"),
array("United Statues", "Los Angeles", "Los Angeles Exchange Program"),
array("China", "Hong Kong", "HK One Exchange Program"),
array("China", "Hong Kong", "HK Two Exchange Program"),
array("China", "Hong Kong", "HK Five Exchange Program"),
array("United States", "Austin", "Austin Exchange Program"),
array("United Kingdom", "Oxford", "Oxford Exchange Program"),
array("United Kingdom", "Cambridge", "Cambridge Exchange Program")
);;
The first level of sort is to sort the countries alphabetically. This is as far as I've gotten. I've used usort() to sort them using:
I'm not sure if that's the optimal way, but it works. In any case, I'm pretty sure that I'll have to scratch that method to do the rest of this. Perhaps, array_multisort() will come in handy?PHP Code:function compare($a, $b){
if ($a == $b) return 0;
return ($a > $b) ? 1 : -1;
}
usort($entries, 'compare');
Anyway, so once the countries are sorted, I'd like the programs within that country to be sorted alphabetically by city. For example, the UK, China, Brazil and Germany have multiple listings. I'd like to sort alphabetically that subset.
Next, there are some cities that have multiple listings. I'd like to sort that subset by the program title (third string in the array).
I know that's confusing. So, in review, I want to:
1. Sort the array by countries (first string)
2. For items that have the same country, I want to sort those by city (second string)
3. For items that have the same city, I want to sort those by program title (third string)
Now, using the usort() function above, I've gotten it to *ALMOST* work. It sorts it all fine except for the very last listing. When I print_r() the array, I get:
Note the highlighted. It's out of place. Everything else is fine. What's going on here? Is there a tweak to the compare function that might fix this? Am I approaching in a totally wrong way?Code:Array ( [0] => Australia [1] => Sydney [2] => Australia Exchange Program ) Array ( [0] => Brazil [1] => Brasilia [2] => Brasilia Exchange Program ) Array ( [0] => Brazil [1] => Rio de Janeiro [2] => Rio Exchange Program ) Array ( [0] => Brazil [1] => Sao Paulo [2] => Sao Paulo Exchange Program ) Array ( [0] => China [1] => Bejing [2] => China Exchange Program ) Array ( [0] => China [1] => Hong Kong [2] => HK Five Exchange Program ) Array ( [0] => China [1] => Hong Kong [2] => HK One Exchange Program ) Array ( [0] => China [1] => Hong Kong [2] => HK Two Exchange Program ) Array ( [0] => Germany [1] => Berlin [2] => Berlin Exchange Program ) Array ( [0] => Germany [1] => Hamburg [2] => Hamburg Exchange Program ) Array ( [0] => India [1] => New Delhi [2] => India Exchange Program ) Array ( [0] => Peru [1] => Lima [2] => Peru Exchange Program ) Array ( [0] => Thailand [1] => Bangkok [2] => Thailand Exchange Program ) Array ( [0] => United Kingdom [1] => Cambridge [2] => Cambridge Exchange Program ) Array ( [0] => United Kingdom [1] => London [2] => UK Exchange Program ) Array ( [0] => United Kingdom [1] => Oxford [2] => Oxford Exchange Program ) Array ( [0] => United States [1] => Austin [2] => Austin Exchange Program ) Array ( [0] => United States [1] => New York [2] => NYC A Exchange Program ) Array ( [0] => United States [1] => New York [2] => NYC Q Exchange Program ) Array ( [0] => United States [1] => New York [2] => NYC X Exchange Program ) Array ( [0] => United States [1] => Philadelphia [2] => Philadelphia Exchange Program )Array ( [0] => United Statues [1] => Los Angeles [2] => Los Angeles Exchange Program )
Thanks guys
Also note that I spelled States wrong. Oh boy...starting too long at code. Move along folks...nothing to see here





Reply With Quote
Bookmarks