Reputation: 87
I'm playing around with opencart and trying to add a few new features to the filters functionality of the cart.
I'm wondering how do I efficiently count the number of occurrences of a given value in a multidimensional php array? For example, I'm trying to work our if all Filter->Count == 0?
Array
(
...
[filter] => Array
(
[0] => Array
(
[filter_id] => 109
[name] => Boyfriends (0)
[count] => 0
)
[1] => Array
(
[filter_id] => 114
[name] => Daughters (0)
[count] => 0
)
[2] => Array
(
[filter_id] => 115
[name] => Fathers (0)
[count] => 0
)
[3] => Array
(
[filter_id] => 108
[name] => For Her (53)
[count] => 53
)
...
)
...
)
Upvotes: 0
Views: 125
Reputation: 12681
$amount = 0;
foreach($array as $value) {
if($value['count'] == 0)
$amount++;
}
That would return the amount of 'counts' that equal to zero. To return the amount that aren't 0, use this:
$amount = 0;
foreach($array as $value) {
if($value['count'] != 0)
$amount++;
}
Then $amount
will equal zero iff all the 'count' values are zero.
Upvotes: 1