[thelist] php array cleverness needed

Steve Lewis nepolon at worlddomination.net
Tue Sep 2 18:11:00 CDT 2003


Dunstan Orchard wrote:

The only thing you appear to be missing is some logic to modify the 
behavior of when you add an item to common_countries.  A little bit of 
good ole top down design might help organize this process a bit.

>[1] Add the top 5 country names by count to an array. The count must be > 0 to 
>be added to the array, so if only the first 2 countries have a count > 0 then 
>only 2 records would be added.
>[2] Sort the array by country name.
>[3] Loop through the array, printing out the country names. 
>  
>
This is one action you want to perform with the result set.  It can be 
broken down into steps:

1) sort $countries by country_count
2) get the top five countries
3) sort the array
4) output the list

step 2: "get_top_five" looks something like:

/*
 * testing 3 conditions:
 * a) next record in result array exists
 * b) country_count returns a positive integer
 * c) less than 5 countries have been selected from the list
 * exit when any of them is false
 */
for($i = 0; $i < $count($countries) && $countries['country_count'] > 0 
&& count($common_countries) < 5; i++)
{
    // add $countries[$i] to $common_countries
}

you are already doing everything else it looks like.

hth
--Steve



More information about the thelist mailing list