简体   繁体   中英

PHP Get Highest Count Value from Array

In an array I have to find the object that is most recurring

$items = array("370","370","546","55");
$value = max($items);
$key = array_search($value, $items);

This print out 2 and 546.

But how can I output 2 and its related value, so in my example 370 ?

I use array_count_values and from the resulting array use array_keys to get the numbers which appear most (eg 370) and array_values to get the number of times it appears (eg 2).

<?php
$items   = [370,370,546,55];
$counted = array_count_values($items);
/*
Array
(
    [370] => 2
    [546] => 1
    [55] => 1
)

*/

$number_appears_most = array_keys($counted);
/*
Array
(
    [0] => 370
    [1] => 546
    [2] => 55
)
*/

$number_of_occurences = array_values($counted);
/*
Array
(
    [0] => 2
    [1] => 1
    [2] => 1
)
*/    
echo $number_appears_most[0] . ' appears ' . $number_of_occurences[0] . ' time(s)' . PHP_EOL;

This will give you:

370 appears 2 time(s)

Demo: https://eval.in/890626

I took @alistaircol Idea and not only fixed it (as it's dependent on the order of the array) but I also simplfied the assignment part so we are not creating 2 new arrays

You can try it here

http://sandbox.onlinephpfunctions.com/code/2fe11fb6040dc68db43fb5cbb858ef3e47394dd2

And here is the code

$items   = [370,370,546,55,55,55];
$counted = array_count_values($items);

arsort($counted); //sort descending maintain keys

$occurences = reset($counted); //get the first value (rewinds internal pointer )
$most_frequent = key($counted); //get the key, as we are rewound it's the first key

echo $most_frequent . ' appears ' . $occurences . ' time(s)' . PHP_EOL;

This correctly outputs

55 appears 3 time(s)

Just FYI @alistaircol original answer would fail this test case

http://sandbox.onlinephpfunctions.com/code/fe13e7a0bd00ea1219bc69e2bc5a5aebaf478034

Which is changing the input array from:

  $items   = [370,370,546,55];

To:

  $items   = [370,370,546,55,55,55];

It would still output:

  370 appears 2 time(s)

In this case when the correct answer is 55 appears 3 time(s)

This way the item with the most is the last one, which exposes that it's based on the order ( which was obvious to me because of using the first index [0] )

Not to call him out on it, but as it was accepted as the answer I felt I should point that out. Overall though I was a sound approach to the question. So Kudos on that.

UPDATE

One way to get all values that show more then one time is like this:

$items   = [370,370,546,55,55,55];

$unique = array_unique( $items );

$diff = array_diff_assoc( $items, $unique);

print_r( $diff );

Which outputs

Array
(
   [1] => 370
   [4] => 55
   [5] => 55
)

You can test it here http://sandbox.onlinephpfunctions.com/code/53df6a8ea7a68768572cfef494e3b715aa13e83b

One note, is that you will get exactly one less occurrence then is actually present.

UPDATE1

We can easily combine these and account for the missing one. See this fiddle to test that

http://sandbox.onlinephpfunctions.com/code/1bc1a30a5e091af3a18fec2c8b48050869108549

And the code:

$items   = [370,370,546,55,55,55];

$unique = array_unique( $items );

$diff = array_diff_assoc( $items, $unique);

print_r( $diff );
echo "\n\n";

$counted = array_count_values($diff);

arsort($counted); //sort descending maintain keys

$occurences = reset($counted); //get the first value (rewinds internal pointer )
$most_frequent = key($counted); //get the key, as we are rewound it's the first key

echo $most_frequent . ' appears ' . ( $occurences + 1 ) . ' time(s)' . PHP_EOL;

Outputs ( both the previous ones )

Array
(
    [1] => 370
    [4] => 55
    [5] => 55
)

55 appears 3 time(s)

Oh and if you want the ones that are more then one as a list with no duplicates, then just hit it again with array unique $unique = array_unique($diff);

Cheers!

You can find same value count like this way.

print_r(array_count_values($items));

will output

Array
(
   [370] => 2
   [546] => 1
   [55] => 1
   etc...
)

To get 2 and 307 value you have to do below things.

$new_arr = array_count_values($items);
$max = max($new_arr);
$new_arr = array_keys($new_arr, max($new_arr)));
echo $new_arr[0]."is repeated (".$max.") times.";

will output

  307 is repeated (2) times.
$items   = [370,370,546,55,55];
$counted = array_count_values($items);  // count occurrences
$counted=array_intersect ($counted,[max($counted)]);  // only keep elements with highest occurrence
var_export ($counted);  // print to screen 

There is a php function array_count_values that should work in your case.

Then you have an array with a count of all values and you can sort and work with them.

https://secure.php.net/manual/en/function.array-count-values.php

$items = array("370","370","546","55");
print_r(array_count_values($items));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM