I have an array of strings and I am looking for a way to find the most common string in the array.
$stuff = array('orange','banana', 'apples','orange');
I would want to see orange.
The array_unique() function removes duplicate values from an array. If two or more array values are the same, the first appearance will be kept and the other will be removed. Note: The returned array will keep the first array item's key type.
$c = array_count_values($stuff); $val = array_search(max($c), $c);
Use array_count_values
and get the key of the item:
<?php $stuff = array('orange','banana', 'apples','orange', 'xxxxxxx'); $result = array_count_values($stuff); asort($result); end($result); $answer = key($result); echo $answer; ?>
Output:
orange
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With