php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #33279 array_count_values casts number-like strings to ints
Submitted: 2005-06-08 19:35 UTC Modified: 2005-06-08 21:48 UTC
From: graced at monroe dot wednet dot edu Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 5.0.4 OS: Linux 2.4.21/RHAS 3
Private report: No CVE-ID:
 [2005-06-08 19:35 UTC] graced at monroe dot wednet dot edu
Description:
------------
See: 
bug #28435 array_count_values() problem
http://www.php.net/manual/en/language.types.array.php

array_count_values() casts numeric strings (i.e. "72") to integers (72).  This is correct (although annoying) behavior as described at http://www.php.net/manual/en/language.types.array.php, except for one catch.

The documentation states (emphasis mine)
"If a key is the standard representation of an integer, it will be interpreted as such (i.e. "8" will be interpreted as 8, ***while "08" will be interpreted as "08"***).

However, array_count_values() will strip the leading zeroes and turn those strings into integers, which causes errors in  code later on when (string) 8  is compared against (string) "08" and the comparison fails.

Reproduce code:
---------------
<?php
var_dump(array_count_values(array("012345")));


Expected result:
----------------
array(1) {
  ["012345"]=>
  int(1)
}


Actual result:
--------------
array(1) {
  [12345]=>
  int(1)
}


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-06-08 21:23 UTC] graced at monroe dot wednet dot edu
For what it's worth, *this* works:

var_dump(array("012345" => 'yay'));
array(1) {
  ["012345"]=>
  string(3) "yay"
}

so it looks like it's an issue with array_count_values() itself and not something at a deeper level.
 [2005-06-08 21:48 UTC] tony2001@php.net
Please do not submit the same bug more than once. An existing
bug report already describes this very problem. Even if you feel
that your issue is somewhat different, the resolution is likely
to be the same. 

Thank you for your interest in PHP.

Please *always* try latest snapshot before reporting a bug.
See bug #30833.
 
PHP Copyright © 2001-2014 The PHP Group
All rights reserved.
Last updated: Fri Apr 25 07:02:14 2014 UTC