PHP: Most frequent value in array
$values = array_count_values($array);
arsort($values);
$popular = array_slice(array_keys($values), 0, 5, true);
array_count_values()
gets the count of the number of times each item appears in an arrayarsort()
sorts the array by number of occurrences in reverse orderarray_keys()
gets the actual value which is the array key in the results fromarray_count_values()
array_slice()
gives us the first five elements of the results
Demo
$array = [1,2,3,4,238, 7, 86, 79, 55, 92, 55, 7, 254, 9, 75, 238, 89, 238];
$values = array_count_values($array);
arsort($values);
$popular = array_slice(array_keys($values), 0, 5, true);
array (
0 => 238,
1 => 55,
2 => 7,
3 => 4,
4 => 3,
)
Getting the most repeated values in an array
This should work:
$count=array_count_values($array);//Counts the values in the array, returns associatve array
arsort($count);//Sort it from highest to lowest
$keys=array_keys($count);//Split the array so we can find the most occuring key
echo "The most occuring value is $keys[0][1] with $keys[0][0] occurences."
PHP Get most repeated value in Array
"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
Find the most frequent number in PHP array
I have an array in PHP with repeating numbers and I would like to find the most frequent but only when there is only one of that.
Your approach seems rather complicated.
Here's how I'd do that:
$numbers = [1, 6, 5, 6, 2, 1, 6, 7, 8]; // positive test case
//$numbers = [1, 6, 5, 6, 2, 1, 6, 7, 8, 1]; // negative test case
$count = array_count_values($numbers); // get count of occurrence for each number
arsort($count); // sort by occurrence, descending
$first = key($count); // get key of first element, because that is the/one
// of the highest number(s)
$count_first = current($count); // get occurrence for first array value
$count_second = next($count); // get occurrence for second array value
if($count_first != $count_second) { // did they occur in different frequencies?
echo $first . ' occurred most in input array.';
}
else {
echo 'input array contained multiple values with highest occurrence.';
}
php - Most frequent element in array?
Your solution seems reasonable.
<?php
echo mostFrequent(array('cheese','wine','cheese','bread','cheese','bread'));
?>
How to enter my string of numbers into an array to get the most frequent number?
Use explode()
to build your array, based on the delimiter ", "
:
$string_of_numbers = "1, 2, 4, 5, 6, 3, 2, 2, 1, 4, 4, 4";
$numbers = explode(", ", $string_of_numbers);
var_dump($numbers);
Output
array (size=12)
0 => string '1' (length=1)
1 => string '2' (length=1)
2 => string '4' (length=1)
3 => string '5' (length=1)
4 => string '6' (length=1)
5 => string '3' (length=1)
6 => string '2' (length=1)
7 => string '2' (length=1)
8 => string '1' (length=1)
9 => string '4' (length=1)
10 => string '4' (length=1)
11 => string '4' (length=1)
Get the most frequent value in a string in PHP
You can do it in four steps:
Use str_split() to create array from string which contains single digits.
Use array_count_values() on this splitted string. It returns array which indexes are your digits and values are count of occurences of this digits.
Use arsort() to sort array.
Get first element of array using reset() and key() functions.
Example:
<?php
$string = '198419489151085175';
$splittedString = str_split($string);
$charactersCount = array_count_values($splittedString);
arsort($charactersCount);
reset($charactersCount);
$firstKey = key($charactersCount);
var_dump($charactersCount);
var_dump($firstKey);
And result is:
array(7) {
[1]=>
int(5)
[9]=>
int(3)
[8]=>
int(3)
[5]=>
int(3)
[4]=>
int(2)
[0]=>
int(1)
[7]=>
int(1)
}
1 // this is your result
Get the most repeated elements inside array, if more than one is the most repeated, get results alphabatically in order PHP
Based on this answer, this will do:
$array = array('orange','apple','banana','apple','orange','grape','orange', 'mango','apple');
$values = array_count_values($array);
$max = max($values);
$fruits = array_filter($values, function($num) use ($max){ return $max == $num; });
$ksort($fruits);
Output:
[
apple => 3,
orange => 3
]
Related Topics
Flex/Grid Properties Are Deleted in Gmail Email
How Do We Implement Custom API-Only Authentication in Laravel
"Warning: MySQL_Query(): Supplied Argument Is Not a Valid MySQL-Link Resource"
What Do \R and \N Mean in PHP (And Other Languages)
Is Using Superglobals Directly Good or Bad in PHP
How to Sftp Upload Files from PHP
Php, Why Do You Escape My Quotes
How to Sort a Multidimensional Array by Multiple Columns
Warning Problem: Expects Parameter 1 to Be MySQLi_Result
Single Result from Sum with MySQLi
Differencebetween $_Files["File"]["Type"] and End(Explode(".", $_Files["File"]["Name"]))
Sparql Query to Get All Parent of a Node
Extract Dom-Elements from String, in PHP
PHP - Fastest Way to Check Presence of Text in Many Domains (Above 1000)