唯一的数组值计数

时间:2014-08-12 09:06:20

标签: php arrays

我有一个名为$ countrySelected的数组。我想检索一下国家的总数。

例如,如果阵列中的阿富汗奥兰群岛阿富汗,唯一的计数将是 2

任何指针,或正确方向的参考将不胜感激。我是否以某种方式合并数组,然后计算唯一值?

Array
(
    [0] => Array
        (
            [0] => Afghanistan
            [1] => Aland Islands
        )

    [1] => Array
        (
            [0] => Aland Islands
            [1] => Albania
            [2] => Algeria
        )

    [2] => Array
        (
            [0] => Albania
            [1] => Algeria
        )

    [3] => 
    [4] => Array
        (
            [0] => Albania
            [1] => Algeria
        )

    [5] => Array
        (
            [0] => Aland Islands
            [1] => Albania
            [2] => Algeria
        )

    [6] => 
    [7] => 
    [8] => 
    [9] => 
    [10] => 
    [11] => Array
        (
            [0] => Afghanistan
        )

    [12] => 
    [13] => 
    [14] => 
    [15] => 
    [16] => 
    [17] => 
    [18] => 
    [19] => Array
        (
            [0] => Albania
            [1] => Algeria
        )

    [20] => 
    [21] => 
    [22] => 
    [23] => 
    [24] => 
    [25] => 
    [26] => 
    [27] => 
    [28] => 
    [29] => 
    [30] => 
    [31] => 
    [32] => 
    [33] => 
    [34] => 
    [35] => 
    [36] => 
    [37] => 
    [38] => 
    [39] => 
    [40] => 
    [41] => 
    [42] => 
    [43] => 
    [44] => 
    [45] => 
    [46] => 
    [47] => 
    [48] => 
    [49] => 
    [50] => 
    [51] => 
    [52] => 
    [53] => 
    [54] => 
    [55] => 
    [56] => 
    [57] => 
    [58] => 
    [59] => 
    [60] => 
    [61] => 
    [62] => 
    [63] => 
    [64] => 
    [65] => 
    [66] => 
    [67] => 
    [68] => 
    [69] => 
    [70] => 
    [71] => 
    [72] => 
    [73] => 
    [74] => 
    [75] => 
    [76] => 
    [77] => 
    [78] => 
    [79] => 
)

5 个答案:

答案 0 :(得分:1)

你可以这样做

$arr_country = array();

foreach($your_array as $arr)
{
    foreach($arr as $country)
    {
       if(!in_array($country, $arr_country))
       {
         $arr_country[] = $country;
       }
    }   
}

echo "Total Countries : ".count($arr_country);

答案 1 :(得分:0)

您可以使用内置数组函数和count来获取简单数组

$ary=array("Af","Bc","Af");
$count=count(array_unique($ary));
echo $count;

用于数组使用

$countrySelected=array(); 
foreach ($resarray as $tkey=>$tvalue)
{
   if(is_array($tvalue))
     { 

        foreach($tvalue as $finkey=>$finvalue)
       {
          $countryselected[]=$finvalue;

       }
    }
}
$count=count(array_unique($countryselected));
echo $count;

答案 2 :(得分:0)

<?php
    $countries = array();    
    foreach($given_array as $array){
        foreach($array as $country){
            if(!in_array($country, $countries)){
                $countries[] = $country;
            }
        }   
    }    
    echo count($countries); // prints Total Number of countries
?>

答案 3 :(得分:0)

我尝试使用array_reduce,使用数组键翻转

$uniques = array_reduce( $countries, function ( $carry, $item){
    return $item ? $carry + array_flip($item) : $carry;
}, array() );

echo count( array_keys( $uniques) );

答案 4 :(得分:0)

这是一个简单的解决方案:无循环:)

$array  = array(array("Afghanistan", "Aland Islands"), array("Aland Islands", "Albania", "Albania"));

$result = array_count_values(call_user_func_array('array_merge', $array));

echo "<pre>";
print_r($result);

<强>计数($结果); //将为您提供独特国家/地区的数量。

输出:

Array
(
    [Afghanistan] => 1
    [Aland Islands] => 2
    [Albania] => 2
)