5

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 can I do it?

4

7 Answers 7

7
$count = 0;
foreach ($array as $key=>$value) {
    if ($value ['item'] == 'Banana Cream Cheesecake') {
        $count++;
    }
}
echo $count;
Sign up to request clarification or add additional context in comments.

2 Comments

I 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?
This 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 use array_count_values() for once only this function spend 0.39 milliseconds.
4
array_count_values(array_map(function($foo){return $foo['item'];}, $arr));

1 Comment

Any answer ought to be specific (match the question) and include "Banana Cream Cheesecake".
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'];

Comments

1

print_r the array and count the number of occurrences of your cheesecake.

No, seriously, without more information, this is a solution ;)

$iCount = substr_count(print_r($array, true), "cheese cake");

3 Comments

thanks! Answers and conditionned by questions, I guess i might do myself differently in other contexts...
Did you change the string/array inside by what you need?
@Navdeep You should do print_r($array, true) so it returns the string and doesn't write in standard output
1

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.

Comments

0

Today, it is simpler to do this.

Here is your array:

$products = 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'
  )
);

Count items:

$counts = array_count_values(array_column($products, 'item'));

var_dump($counts);

Demo Here

2 Comments

Re "Today": Can you be more specific? What PHP version was this tried on? What is the minimum PHP version? When was it introduced? Please respond by editing (changing) your answer, not here in comments (but *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** without *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** "Edit:", "Update:", or similar - the answer should appear as if it was written today).
Any answer ought to be specific (match the question) and include "Banana Cream Cheesecake".
-1

Get an issue when dealing with numbers instad of just strings, due to the fact that substr_count takes also the keys, so I used just plain count instead of substr_count and worked fine.

$icount=count(print_r($_SESSION['id_bra'], true), $bran);

1 Comment

This does not answer the question. Any answer has to include "Banana Cream Cheesecake" (or the equivalent).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.