PHP获取Array中最重复的值

时间:2015-11-07 09:56:09

标签: php arrays

我在这样的数组中有一个数组:

Array
(
    [0] => Array
        (
            [name] => B
            [id] => 924572
        )

    [1] => Array
        (
            [name] => A
            [id] => 120689
        )

    [2] => Array
        (
            [name] => A 
            [id] => 120689
        )

    [3] => Array
        (
            [name] => C
            [id] => 919644
        )

    [4] => Array
        (
            [name] => A
            [id] => 120689
        )

    [5] => Array
        (
            [name] => B
            [id] => 924572
        )
)

如何从名为nameid的对象中获取最重复的值?

我已经尝试过以下代码,但收到错误:Warning: array_count_values(): Can only count STRING and INTEGER values!

$count = array_count_values($info);
arsort($count);
$popular = array_keys($count);
echo $popular[0];

有关此问题的任何解决方法?

6 个答案:

答案 0 :(得分:1)

基于PHP中的finding the modemapping。这会有用吗?

   public View getView(int position, View convertView, ViewGroup parent) {
     ImageView imageView;

   if (convertView == null) {
        imageView = new ImageView(mContext);
       imageView.setLayoutParams(new GridView.LayoutParams(400, 400));
       imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setPadding(8, 8, 8, 8);
        } else {
      imageView = (ImageView) convertView;
   }

     imageView.setImageResource(mThumbIds[position]);

     return imageView;
    }

要返回var kmlFile; var url = "MultipleKML_TKHH.ashx"; if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari XMLHttpRequestObject = new XMLHttpRequest(); } else {// code for IE6, IE5 XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP"); } XMLHttpRequestObject.open("GET", url, false); XMLHttpRequestObject.send(); kmlFile = XMLHttpRequestObject.responseText; //alert(kmlFile); $name_array = array_map(function($x) {return $x["name"];}, $info); $count = array_count_values($name_array); $mode = array_keys($count, max($count)); 对数组,请使用:

"name"

答案 1 :(得分:1)

也许你可以使用这个解决方案:

<?php
$info = array(
    array(
        "name" => "B",
        "id" => 924572
    ),
    array(
        "name" => "A",
        "id" => 120689
    ),
    array(
        "name" => "A",
        "id" => 120689
    ),
    array(
        "name" => "C",
        "id" => 919644
    ),
    array(
        "name" => "A",
        "id" => 120689
    ),
    array(
        "name" => "B",
        "id" => 924572
    ),
);

$result = array();
foreach ($info as $infoKey => $infoValue) {
    foreach ($infoValue as $itemKey => $itemValue) {
        if ($itemKey != "name") {
            continue;
        }
        if (array_key_exists($itemValue, $result)){
            $result[$itemValue]++;
            continue;
        }
        $result[$itemValue] = 1;
    }
}
arsort($result);
var_dump($result);

将导致:

array (size=3)
  'A' => int 3
  'B' => int 2
  'C' => int 1

答案 2 :(得分:1)

更线性的解决方案。

$arr = Array
(
    Array
        (
            "name" => "B",
            "id" => 924572
        ),

    Array
        (
            "name" => "A",
            "id" => 120689
        ),

    Array
        (
            "name" => "A" ,
            "id" => 120689
        ),

    Array
        (
            "name" => "C",
            "id" => 919644
        ),

    Array
        (
            "name" => "A",
            "id" => 120689
        ),

    Array
        (
            "name" => "B",
            "id" => 924572
        ));

$countArr = Array();

for($i = 0; $i < count($arr); $i++)
{
    $tmpArr = $arr[$i];

    if(array_key_exists($tmpArr["name"],$countArr))
        $countArr[$tmpArr["name"]]++;
    else
        $countArr[$tmpArr["name"]] = 0;
}

arsort($countArr);
var_dump($countArr);

答案 3 :(得分:1)

使用array_column(需要PHP 5.5或shim)。

$count_values = array_count_values(array_column($array, 'name'));

$most_frequent_name = array_search(max($count_values), $count_values);


然后,如果您想要所有具有此名称的数组:

$items = array_filter($array, function ($v) use ($most_frequent_name) {
    return $v['name'] == $most_frequent_name;
});


如果多个名称可能具有相同的最高频率:

$count_values = array_count_values(array_column($array, 'name'));

$most_frequent_names = array_keys($count_values, max($count_values));

$items = array_filter($array, function ($v) use ($most_frequent_names) {
    return in_array($v['name'], $most_frequent_names);
});

答案 4 :(得分:1)

&#34;序列化方式&#34;用于搜索大多数重复的夫妻(nameid):

$out = array();
foreach($arr as $el){
   $key = serialize($el);
   if (!isset($out[$key]))
       $out[$key]=1;
   else
       $out[$key]++;
}

arsort($out);

foreach($out as $el=>$count){
   $item = unserialize($el);
   echo "Name = ".$item['name'].' ID = '.$item['id'].' Count = '.$count.'<br/>';
}

输出:

Name = A ID = 120689 Count = 3
Name = B ID = 924572 Count = 2
Name = C ID = 919644 Count = 1

更新没有循环

.....
arsort($out);

$most  = unserialize(key($out));
$most_count = array_shift($out);

echo $most['name'];
echo $most['id'];
echo $most_count;

输出:

A
120689
3

答案 5 :(得分:0)

尝试以下代码。它将为您提供所有元素的出现次数

function array_icount_values($arr,$lower=true) { 
     $arr2=array(); 
     if(!is_array($arr['0'])){$arr=array($arr);} 
     foreach($arr as $k=> $v){ 
      foreach($v as $v2){ 
      if($lower==true) {$v2=strtolower($v2);} 
      if(!isset($arr2[$v2])){ 
          $arr2[$v2]=1; 
      }else{ 
           $arr2[$v2]++; 
           } 
    } 
    } 
    return $arr2; 
} 

$arr = array_icount_values($array);
echo "<pre>";
print_r($arr);