0

I'm looking for a way to count occurence on an array of array.

This is my array :

Array
(
[0] => Array
    (
        [id] => 671
        [title] => BIEND
        [img] => 
        [ville] => marseille
    )

[1] => Array
    (
        [id] => 670
        [title] => BIENC
        [img] => 
        [ville] => avignon
    )

[2] => Array
    (
        [id] => 669
        [title] => BIENB
        [img] => 
        [ville] => avignon
    )

)

And what I would like to have :

Array
(
[avignon] => 2
[marseille] => 1
)

I tried with array_count_values, but it dont seems to be the good way.

Any idea?

gen_Eric
  • 223,194
  • 41
  • 299
  • 337
Vincent Guyader
  • 2,927
  • 1
  • 26
  • 43

4 Answers4

2

You could just go through it manually:

$result = array();
foreach($input as $item) 
{
  $result[$item['ville']]++;
}

or, slightly nicer perhaps,

$result = array();
foreach($input as $item) 
{ 
  $city = $item['ville'];
  if(!array_key_exists($city, $result)) {
    $result[$city] = 1;
  } else {
    $result[$city]++;
  }
}

Alternatively, you could do some array_map magic to first get an array with all the cities, and then use array_count_values as you planned:

$cities = array_count_values( array_map( function($a) { return $a['ville']; } ) );

Note, I haven't tested this last solution, I personally think the first one expresses the intention better. If you would like to use this one because it is shorter (i.e. less readable) I'll leave it to you to debug and comment it

CompuChip
  • 9,143
  • 4
  • 24
  • 48
  • 1
    FYI, since PHP 5.5 you can _pluck_ values via [`array_column()`](http://us3.php.net/manual/en/function.array-column.php), otherwise the concise `array_map` is definitely readable and understandable once you get used to the basics of functional programming [which everybody should get used too imo ^^]. – moonwave99 Feb 26 '14 at 16:01
1

You can use array_reduce():

$data = Array
(
0 => Array
    (
        'id' => 671,
        'title' => 'BIEND',
        'img' => '',
        'ville' => 'marseille'
    )
,
1 => Array
    (
        'id' => 670,
        'title' => 'BIENC',
        'img' => '',
        'ville' => 'avignon'
    )
,
2 => Array
    (
        'id' => 669,
        'title' => 'BIENB',
        'img' => '',
        'ville' => 'avignon'
    )

);

$result = array_reduce($data, function(&$cur, $x)
{   
   $cur[$x['ville']] = isset($cur[$x['ville']])?$cur[$x['ville']]+1:1;
   return $cur;
}, []);
Alma Do
  • 37,009
  • 9
  • 76
  • 105
0
$my_array = array(...);
$result = array();

foreach ($my_array as $arr) {
    $key = $arr['ville'];
    if (! array_key_exists($key, $result){
        $result[$key] = 1;
        continue;
    }
    $result[$key] += 1;
}
Ulrich Horus
  • 352
  • 3
  • 9
-1

I would write something like this. Array and subArray should be renamed according to their content.

   $villes = array();

   foreach($yourArray as $subArray) {
       if(!in_array($subArray['ville'], $villes)) {
           $villes[$subArray['ville']] = 1;
       } else {
           $villes[$subArray['ville']]++;
       }
   }

   var_dump($villes);