how to count the number of items in a php associative array
18,205
Solution 1
$count = 0;
foreach ($array as $key=>$value) {
if ($value ['item'] == 'Banana Cream Cheesecake') {
$count++;
}
}
echo $count;
Solution 2
array_count_values(array_map(function($foo){return $foo['item'];}, $arr));
Solution 3
Given an array like this:
$arr = array(array('item' => 'Banana Cream Cheesecake',
'item_id' => 3,
'product' => 'Desserts'),
array('item' => 'Banana Cream Cheesecake',
'item_id' => 3,
'product' => 'Desserts'),
array('item' => 'Banana Cream Cheesecake',
'item_id' => 3,
'product' => 'Desserts'),
array('item' => 'Banana Cream Cheesecake',
'item_id' => 3,
'product' => 'Desserts'),
array('item' => 'Milk',
'item_id' => 2,
'product' => 'Soda'),
array('item' => 'Banana Cream Cheesecake',
'item_id' => 3,
'product' => 'Desserts'));
You could use array_count_values and get the count by its name
$counted = array_count_values(array_map(function($value){return $value['item'];}, $arr));
echo $counted['Banana Cream Cheesecake'];
Or modify the function to work with the item's id:
$counted = array_count_values(array_map(function($value){return $value['item_id'];}, $arr));
// The item_id
echo $counted['3'];
Solution 4
Expanding on @steve's answer
$counted = array_count_values(array_map(function($foo){return $foo['item'];}, $arr));
This gives you an array with totals of each value for the ['item']
key.
For example, this is the array your example would return:
$counted = Array
(
['Banana Cream Cheesecake'] => 5
['Milk'] => 1
)
Then, to print it's just a matter of echoing that variable:
echo 'Banana Cream Cheesecake = ' . $counted['Banana Cream Cheesecake'];
//prints "Banana Cream Cheesecake = 5"
The advantage here is that you don't need to run through a for loop for each value you need to count - instead you have an array with totals for all values for that specific key.
Solution 5
print_r the array and count the number of occurrences of your cheese cake.
No, seriously, without more infos this is a solution ;)
$iCount = substr_count(print_r($array, true), "cheese cake");
Related videos on Youtube
Author by
Navdeep
Updated on June 04, 2022Comments
-
Navdeep almost 2 years
I have an array as below.
Array ( [0] => Array ( [item] => Banana Cream Cheesecake [item_id] => 3 [product] => Desserts ) [1] => Array ( [item] => Banana Cream Cheesecake [item_id] => 3 [product] => Desserts ) [2] => Array ( [item] => Banana Cream Cheesecake [item_id] => 3 [product] => Desserts ) [3] => Array ( [item] => Banana Cream Cheesecake [item_id] => 3 [product] => Desserts ) [4] => Array ( [item] => Milk [item_id] => 2 [product] => Soda ) [5] => Array ( [item] => Banana Cream Cheesecake [item_id] => 3 [product] => Desserts ) )
I want to print :-
Banana Cream Cheesecake = 5
how to do it?
-
Adam Thornton almost 12 yearsI would hope for more re usability he would actually want an array of totals rather than just knowing how many of that specific dessert there are?
-
Sebas almost 12 yearsthanks! Answers and conditionned by questions, I guess i might do myself differently in other contexts...
-
Sebas almost 12 yearsDid you change the string/array inside by what you need?
-
Michael Laffargue almost 12 years@Navdeep You should do
print_r($array, true)
so it returns the string and doesn't write in standard output -
Arsha almost 7 yearsThis answer is work faster than
array_count_values()
My code use something like this for 8 times. Spend total 0.22 milliseconds. Then I change to usearray_count_values()
for once only this function spend 0.39 milliseconds.