0

I have an array like so:

$resellers =  Array
(
    [40] => Array
        (
            [state] => AB
            [city] => Calgary
        )

    [185] => Array
        (
            [state] => AB
            [city] => Calgary
        )

    [141] => Array
        (
            [state] => AB
            [city] => Calgary
        )

    [41] => Array
        (
            [state] => AB
            [city] => Grande Prairie
        )

    [68] => Array
        (
            [state] => BC
            [city] => Burnaby
        )

    [51] => Array
        (
            [state] => BC
            [city] => Campbell River
        )

    [61] => Array
        (
            [state] => BC
            [city] => Kamloops
        )
  )

I was wondering if there was an easy way for me to count how many values were for "Alberta" (which would be 4), and BC (which would be 3).

hakre
  • 193,403
  • 52
  • 435
  • 836
Jiminy Cricket
  • 908
  • 1
  • 9
  • 24
  • 2
    `foreach` and incrementing a counter isn't easy enough? – Jon Nov 16 '12 at 14:23
  • Possible Duplicate question http://stackoverflow.com/questions/2442230/php-getting-unique-values-of-a-multidimensional-array – Toretto Nov 16 '12 at 14:26
  • What did you try so far? And what doesn't work from the *existing* QA material we have on the site already? For example [Counting Values in Multidimensional Array](http://stackoverflow.com/q/10215659/367456)? Or [Count specific values in multidimensional array](http://stackoverflow.com/q/11558397/367456)? – hakre Nov 16 '12 at 14:28
  • I'd prefer to keep the code as simple and non-looping as possible. – Jiminy Cricket Nov 16 '12 at 14:43
  • 1
    @JiminyCricket: We like to keep this site as non-looping as possible, too. If you miss some answer with existing QA, post a comment there. As written, you're welcome to do your part of the homework first. – hakre Nov 16 '12 at 14:45
  • @JiminyCricket: Please leave your answer as an answer, not as part of the question. I've removed that part. You can still find it in the edit history. – hakre Nov 16 '12 at 14:47
  • Can you please stop editing my content hakre – Jiminy Cricket Nov 16 '12 at 14:49

4 Answers4

11
array_count_values(array_map(function($item) {
   return $item['state'];
}, $resellers));
Cedrun
  • 457
  • 2
  • 8
1

Just iterate through it to find the states you need.

function countState($data, $state) {
    $i = 0;
    foreach($data as $point)
        if($point['state'] === $state)
            $i++;

    return $i;
}

echo "Alberta found " . countState($array, "AB") . " times"!
nyson
  • 1,055
  • 6
  • 20
0

Try:

$arr = array(array('state'=>'AB', 'city'=>'Calgary'), ...); //your array 

function count_values( $arr, $value) {
 $count = 0;
 foreach($arr as $v) {
  foreach($v as $m) {
   if( $m['state'] === $value )
    $count++;
  }
 }
 return $count;
}

$count_ab = count_values($arr, 'AB');
$count_bc = count_values($arr, 'BC');
Teena Thomas
  • 5,139
  • 1
  • 13
  • 17
0

Solved with help from Cedrun's response.

Built array:

 $state_count = array_count_values( array_map(function($item){return $item['state'];}, $r_resellers));

Then to output:

echo $state_count["AB"];
Jiminy Cricket
  • 908
  • 1
  • 9
  • 24