View Full Version : Array Posting

05-04-2007, 01:15 AM
Hi all!

I have a question about Arrays.

I have an array being assigned values by a loop:

if ($myrow = mysql_fetch_array($result)) {
do {

$ip_country = 'United States'; //This value is actually being taken from the database, just trying to keep it simple for here.

$countries[] = $ip_country;

} while ($myrow = mysql_fetch_array($result));

I would like to print out the values on the Array, whatever they may be, and how many times each value is placed into it.


United States 800 times.
United Kigndom 700 times.

I understand how to get everything to work, except how to echo the value only once and have it count the times it shows.

I may be new to the forum, but I have a strong understanding of PHP. Arrays have just been the one thing I have tried to avoid.


05-04-2007, 01:29 AM
If your goal is to obtain a count of each country in your database, it is best to get the database to do this for you in a query. A query will do this very quickly. Fetching all of your data into an array and then using an array function to count the number of times each country appears is the slowest possible way of doing this.

If you insist on using an array, if you research the array functions in the PHP manual http://php.net/array you will find that the array_count_values() function will do what you want.

05-04-2007, 01:35 AM
thanks for the fast reply.

My database actually does not have the countries in it, I am using GeoIP to pick up the ip and verifying it against known ranges for each country. This is so I can verify the location of each user who inputs data on my site.

I tried using print_r(array_count_values($countries)); but that did not give me the values separated just

Array ( [United States] => 385 [Uruguay] => 1 [Venezuela] => 10 [Puerto Rico] => 61 [China] => 1 [Peru] => 2 [United Kingdom] => 1 [Canada] => 2 [Spain] => 1 )

Sorry, I should have mentioned I had tried that.

05-04-2007, 01:42 AM
You should be able to access each country like this:

$countries = array_count_values($countries);
print $countries['United States']; //Outputs 385

...right? ..or am I missing something


05-04-2007, 01:42 AM
If your issue is how to iterate through the array and produce some output for each array index, see the foreach() function - http://php.net/foreach

05-04-2007, 02:10 AM
My database actually does not have the countries in itYou might want to add a column for this. It will save on all the extra processing needed every time you want to obtain a count.

If you complete your code using the current method, benchmark the time it takes to produce a count. Then add a column for the country and populate it. Then recheck the timing necessary to produce a count using a query. If you don't want to keep the database method, you can just delete the country column.

05-04-2007, 02:36 AM
so what youre saying is that instead of figuring out where they came from, just add it to the database as they enter it?

Alright, I just thought that it would be easier to do it this way since I've already got about 500 entries that I need to do this for. but yeah it makes sense since then all i'd have to do is group them through mySQL.

I'll just write a script to update the blank fields.


05-04-2007, 05:58 AM
thanks, i took the advice and it works great... I just needed a new approach:thumbsup: