Counting multidimensional arrays

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
User avatar
doughemi
Forum Newbie
Posts: 11
Joined: Wed Dec 02, 2009 1:40 pm

Counting multidimensional arrays

Post by doughemi »

I have an array in PHP in the format

Code: Select all

Array (     
[0] => Array ( [class] => 14  [Owner] => 89)
[1] => Array ( [class] => 15  [Owner] => 6634)    
[2] => Array ( [class] => 15  [Owner] => 6837)
...
)
I would like to get a count of how many elements are in each class; e.g.how many class 15 elements are in the array.

TIA,
Doug
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Counting multidimensional arrays

Post by Celauran »

Code: Select all

$count = 0;
foreach ($foo as $bar)
{
    if ($bar['class'] == 15)
    {
        $count++;
    }
}
User avatar
doughemi
Forum Newbie
Posts: 11
Joined: Wed Dec 02, 2009 1:40 pm

Re: Counting multidimensional arrays

Post by doughemi »

Thank you. You're my New Best Friend.
Post Reply