Formatting my array ....

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
james_withers
Forum Newbie
Posts: 6
Joined: Mon Aug 04, 2008 8:03 am

Formatting my array ....

Post 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
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Formatting my array ....

Post 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
adroit
Forum Commoner
Posts: 37
Joined: Fri Aug 08, 2008 1:25 am
Location: India
Contact:

Re: Formatting my array ....

Post by adroit »

Use array_unique($array) to get unique values in array.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Formatting my array ....

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
james_withers
Forum Newbie
Posts: 6
Joined: Mon Aug 04, 2008 8:03 am

Re: Formatting my array ....

Post by james_withers »

Thanks (everyone) for the advice. Will check out the loop method and the array functions ...

James
Post Reply