tokis
tokis

Reputation: 125

PHP Get most repeated value in Array

I have an array within an array like this:

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
        )
)

How can I get the most repeated value from object named name and id?

I've already tried the code below but I'm getting an error: 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];

Any fix regarding to this problem?

Upvotes: 2

Views: 1788

Answers (6)

voodoo417
voodoo417

Reputation: 12111

"Serializing way" for searching most repeated couples (name,id):

$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/>';
}

Output:

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

update Without loop

.....
arsort($out);

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

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

Output:

A
120689
3

Upvotes: 1

Gras Double
Gras Double

Reputation: 16383

Makes use of array_column (requires PHP 5.5 or shim).

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

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


Then if you want all arrays with this name:

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


If several names may have the same top frequency:

$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);
});

Upvotes: 1

jayant
jayant

Reputation: 2389

Based on finding the mode and mapping in PHP. Would this work?

$name_array = array_map(function($x) {return $x["name"];}, $info);
$count = array_count_values($name_array);
$mode = array_keys($count, max($count));

To return an array of "name", "id" pairs use:

$return_value = array_filter($info, function($x) use ($mode) { return (in_array($x["name"], $mode));});

Upvotes: 1

Suyog
Suyog

Reputation: 2482

Try following code. It will give you count of occurrences of all elements

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);

Upvotes: 0

The fourth bird
The fourth bird

Reputation: 163467

Maybe you can work with this solution:

<?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);

Will result in:

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

Upvotes: 1

mrhn
mrhn

Reputation: 18946

A more linear solution.

$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);

Upvotes: 1

Related Questions