1

Here is an array with only strings:

$array = ["1", "hello", "1", "world", "hello"];

Notice that "1" is a string.

If I use array_count_values on this array, it seems it silently converts strings to ints.

var_dump(array_count_values($array));

Echos:

array(3) { [1]=> int(2) ["hello"]=> int(2) ["world"]=> int(1) }

1 is now an int. I feel it's a weird behavior. Is it normal? Did I missed something? Is there a way to avoid this behavior?

rap-2-h
  • 30,204
  • 37
  • 167
  • 263
  • 3
    This might actually not be `array_count_values`, but the mechanism of arrays turning numeric indices into numbers. – deceze Mar 13 '18 at 11:04
  • Look this question maybe : https://stackoverflow.com/questions/4100488/a-numeric-string-as-array-key-in-php or https://stackoverflow.com/questions/3445953/how-can-i-force-php-to-use-strings-for-array-keys – Mickaël Leger Mar 13 '18 at 11:07
  • @iainn Not really a duplicate I think. Since it's about a specific function (that roughly turns key into values), I feel it could help other users. Still, it's a good link, thank you. – rap-2-h Mar 13 '18 at 11:11
  • @deceze Ok thanks. I think you could post your comment as an answer with some links to relative question and I will accept it (or I could write it if you prefer) – rap-2-h Mar 13 '18 at 11:12

0 Answers0