2012-07-29 118 views
1

我有以下阵列,这是嵌套在一个更大的阵列阵列的负载:与嵌套计数数组的值

Array 
(
    [0] => Array 
     (
      [vote_for] => 15 
     ) 

    [1] => Array 
     (
      [vote_for] => 15 
     ) 

    [2] => Array 
     (
      [vote_for] => 15 
     ) 

    [3] => Array 
     (
      [vote_for] => 5 
     ) 

    [4] => Array 
     (
      [vote_for] => 5 
     ) 

    [5] => Array 
     (
      [vote_for] => 2 
     ) 

    [6] => Array 
     (
      [vote_for] => 2 
     ) 

    [7] => Array 
     (
      [vote_for] => 2 
     ) 

    [8] => Array 
     (
      [vote_for] => 2 
     ) 

    [9] => Array 
     (
      [vote_for] => 2 
     ) 

    [10] => Array 
     (
      [vote_for] => 2 
     ) 

    [11] => Array 
     (
      [vote_for] => 2 
     ) 

    [12] => Array 
     (
      [vote_for] => 2 
     ) 

    [13] => Array 
     (
      [vote_for] => 2 
     ) 

    [14] => Array 
     (
      [vote_for] => 2 
     ) 

) 

我想要做array_count_values的此阵列上的等价物,使得我得到15 => 3,5 => 22 => 10。我如何取消嵌套数组来做到这一点?

回答

2

你可能想尝试改革的数组数:

$count_array = array(); 
foreach ($arr as $v) { 
    $count_array[] = $v['vote_for']; 
} 

// Now get the counts 
$the_count = array_count_values($count_array); 
+0

感谢PhpMyCoder工作。我从一个不同的方法开始,然后在中途改变,忘记更新'foreach()'到不同的级别。非常感激! – David 2012-07-29 02:52:35

+0

谢谢大卫,完美的答案。 – dplanet 2012-07-29 03:04:42

1

我觉得阵图也将为此

$count_array = array_map(function($item) { return $item['vote_for']; }, $array); 
$the_count = array_count_values($count_array);