2

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?

1
  • @jayant this is php. Commented Nov 7, 2015 at 10:08

6 Answers 6

1

"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
Sign up to request clarification or add additional context in comments.

1 Comment

This is the best answer so far! Thank you! Let me ask a question. How can I display first result without using foreach($out as $el=>$count)? I've tried to used echo $out[1] but nothing displayed.
1

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

5 Comments

Your code works but in my actual array it doesn't work. Here's my actual array: pastebin.com/raw.php?i=Yj1MvTa2
And what is the difference? i guess name is defined a a string and keys the same way?
Then it should work, what error do you get on my code?
Sorry. Your code really works. Thank you! But is it possible to also add the "id" object in array?
You want id added to the sorted count result?
1

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

3 Comments

It only returns: array(0) { }
Whoops, I forgot to add the array itself. Updated the code.
Nice! Your code works well! But is it possible to also add the "id" object in array?
1

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

7 Comments

There's still an error "Warning: array_count_values(): Can only count STRING and INTEGER values! "
Sorry I was accessing the array elements incorrectly. I have fixed my answer to use $x["name"]
@tokis yes. Updated my answer to return all values
That's great! Thanks! But is it possible to also add the "id" object in array?
@tokis use array_filter and in_array as updated in my answer
|
1

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

Comments

0

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

1 Comment

It's not sorting from highest to lowest (MOST REPEATED VALUE).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.