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?

4
  • 3
    This might actually not be array_count_values, but the mechanism of arrays turning numeric indices into numbers. Commented Mar 13, 2018 at 11:04
  • Look this question maybe : stackoverflow.com/questions/4100488/… or stackoverflow.com/questions/3445953/… Commented Mar 13, 2018 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. Commented Mar 13, 2018 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) Commented Mar 13, 2018 at 11:12

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.