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. 我如何取消嵌套数组来做到这一点?

4

2 回答 2

2

您可能想尝试重新排列您的数组以计数:

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

// Now get the counts
$the_count = array_count_values($count_array);
于 2012-07-29T02:50:46.407 回答
1

我认为数组映射也适用于此

$count_array = array_map(function($item) { return $item['vote_for']; }, $array);
$the_count = array_count_values($count_array);
于 2012-07-29T03:17:17.587 回答