I'm using PHP Codeigniter, and I'm trying to get a string of numbers that are in a single variable to an array where it will produce the most frequent number.
I'm trying to do this but it doesn't seem to work.
$string_of_numbers = "1, 2, 4, 5, 6, 3, 2, 2, 1, 4, 4, 4";
$numbers = [$string_of_numbers];
But this works
$numbers = [1, 2, 4, 5, 6, 3, 2, 2, 1, 4, 4, 4];
I think I'm supposed to convert those string numbers into an interval, but I'm not sure how.
$strong_of_numbers = "1, 2, 4, 5, 6, 3, 2, 2, 1, 4, 4, 4";
$numbers = [$strong_of_numbers ];
$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
$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.';
}
End result is that $numbers should accept the $strong_of_numbers as integers, and show the most common/frequent integer.