Page 1 of 1

Formatting my array ....

Posted: Wed Oct 08, 2008 5:51 am
by james_withers
Hi,

I have a massive array containing dates. This array is unsorted and there are lots of duplicates. I need to create a report that displays this array as a kind of histogram based on number of occurences (how many of each date). For example in the array I have:
2007-06-22
2007-06-23
2007-06-23
2007-06-23
2007-06-24
2007-06-24

I want to format this information so it looks like below:

2007-06-22 1
2007-06-23 3
2007-06-24 2

The dates in the array are in no particularly order and I would also like to format the results so they are in date order. I then want to use the number of occurence data to create a line graph but need to get to this stage first. Can anyone advise the best way to do this?

Thanks

James

Re: Formatting my array ....

Posted: Wed Oct 08, 2008 5:54 am
by josh
http://us3.php.net/manual/en/ref.array.php
Array functions would be the best way, assuming you couldn't use objects, or a normalized database to do this

Re: Formatting my array ....

Posted: Wed Oct 08, 2008 5:58 am
by adroit
Use array_unique($array) to get unique values in array.

Re: Formatting my array ....

Posted: Wed Oct 08, 2008 10:01 am
by pickle
adroit wrote:Use array_unique($array) to get unique values in array.
That'll torch the counts he's looking for.

Something like this should work;

Code: Select all

foreach($whateverYourArrayIs as $date)
{
   $counts[$date]++;
}
ksort($counts);
That might cause notices to be thrown - not sure.

Re: Formatting my array ....

Posted: Wed Oct 08, 2008 10:43 am
by james_withers
Thanks (everyone) for the advice. Will check out the loop method and the array functions ...

James