0

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.

Armil
  • 21
  • 2
  • 1
    Building arrays doesn't work like that but you can explode the string to build your array. `$numbers = explode(', ', $strong_of_numbers);` http://php.net/manual/en/function.explode.php – Second2None Jan 26 '19 at 23:41

3 Answers3

0

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)
Cid
  • 14,968
  • 4
  • 30
  • 45
  • That really helped me a lot! Thanks. I don't manipulate arrays so often like this, but this will be remembered for future instances. – Armil Jan 27 '19 at 13:04
  • @ArmilPurificacion practicing is the key. Try, fail, learn, that's how one progress – Cid Jan 27 '19 at 14:09
0

You can use the explode function for that combining with casting it to integers by array mapping would be elegant.

$string_of_numbers = "1, 2, 4, 5, 6, 3, 2, 2, 1, 4, 4, 4";
$numbers = array_map('intval', explode(',', $string_of_numbers));

var_dump($numbers);

This will return

array(12) {
  [0] => int(1)
  [1] => int(2)
  [2] => int(4)
  [3] => int(5)
  [4] => int(6)
  [5] => int(3)
  [6] => int(2)
  [7] => int(2)
  [8] => int(1)
  [9] => int(4)
  [10] => int(4)
  [11] => int(4)
}

In general it's good practice to have a var of a type that it represents. Storing int's as strings is not a good idea, using strict checking on conditions will fail and you are most likely dealing with auto castings when working with the result set in PHP. That's why I recommend using the array map to strictly type it to int. That way you know exactly what it is, your new source $numbers is what it is, it's a collection of individual numbers, in this case an array of integers.

Sanne
  • 1,116
  • 11
  • 17
0

Use explode() to convert string to an array

$string_of_numbers = "1, 2, 4, 5, 6, 3, 2, 2, 1, 4, 4, 4";
$make_array = explode(',', $string_of_numbers);

More about explode()

$count = array_count_values($string_of_numbers);

//$print_r(array_count_values($string_of_numbers))
Array ( [1] => 2 [2] => 3 [4] => 4 [5] => 1 [6] => 1 [3] => 1 ) 

$asort = arsort($count);
//echo arsort($count) = 1

$first = key($count);
//echo key($count); = 4

$count_first = current($count);
// echo current($count); = 4

$count_second = next($count);
//echo next($count); = 3
Danish Ali
  • 2,354
  • 3
  • 15
  • 26