1

i have an array like this:

array(3) { 
[0]=> array(4) { ["user_id"]=> int(1) ["date"]=> string(10) "16-05-2014" ["time"]=> string(8) "21:19:50" ["product"]=> int(70) } 
[1]=> array(4) { ["user_id"]=> int(1) ["date"]=> string(10) "16-05-2014" ["time"]=> string(8) "21:21:05" ["product"]=> int(76) } 
[2]=> array(4) { ["user_id"]=> int(1) ["date"]=> string(10) "16-05-2014" ["time"]=> string(8) "21:22:30" ["product"]=> int(70) } 
}

How can i user array_count_values with 2 lots of data from the array?

I would like to count where user_id = 1 and product = 70

Thanks

2
  • That is not how array_count_values works. You would probably want to use array_filter to get an array where only those values are what you are looking for and get a count of that. Or just loop over and make a count. Perhaps even array_reduce would work well. Commented May 16, 2014 at 21:44
  • 1
    foreach, a counter and two ifs should be enough. Commented May 16, 2014 at 21:46

2 Answers 2

2

Using array_reduce:

<?php
$array = array(
    array('user_id'=>1, 'product'=>70),
    array('user_id'=>2, 'product'=>70),
    array('user_id'=>3, 'product'=>70),
    array('user_id'=>1, 'product'=>55),
    array('user_id'=>1, 'product'=>70),
    );

$user_id = 1;
$product = 70;

$count = array_reduce($array,
                        function($prev, $item) use ($user_id, $product){
                            //if the two numbers match
                            if($item['user_id'] == $user_id && $item['product'] == $product){
                                //add one to the previous value
                                $prev++;
                            }
                            return $prev;
                        },
                        0);

var_dump($count);

Demo

Sign up to request clarification or add additional context in comments.

3 Comments

return $prev + ($item['user_id'] == $user_id && $item['product'] == $product); is much shorter...
@bwoebi shorter != better. I would say you sacrifice readability by smashing it all on one line. I was originally going to use a ternary but decided for sake of making it easier for OP to understand what was happening. You also have to know that the boolean result would implicitly convert to the integer 0/1.
That's something I automatically assume since I'm working with C where a comparison result is always 0/1... Might depend on the experience if that's readable or not.
1

Use a simple foreach to loop the array and count where the data matches.

<?php
//Thanks to Jonathan Kuhn
$array = array(
    array('user_id'=>1, 'product'=>70),
    array('user_id'=>2, 'product'=>70),
    array('user_id'=>3, 'product'=>70),
    array('user_id'=>1, 'product'=>55),
    array('user_id'=>1, 'product'=>70),
    );

$user_id = 1;
$product = 70;

//Actual code
$count = 0;
foreach($array as $val) {
  if($val['user_id'] == $user_id && $val['product'] == $product)
    $count++;
}

var_dump($count);
?>

The end result is the same as for Jonathans, but this is more clear.

Codepad example

Comments

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.