php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #28435 array_count_values() problem
Submitted: 2004-05-18 19:04 UTC Modified: 2004-10-23 20:23 UTC
Votes:2
Avg. Score:5.0 ± 0.0
Reproduced:2 of 2 (100.0%)
Same Version:1 (50.0%)
Same OS:1 (50.0%)
From: ofirin at yahoo dot com Assigned:
Status: Closed Package: Arrays related
PHP Version: 5.0.1 OS: Any
Private report: No CVE-ID: None
 [2004-05-18 19:04 UTC] ofirin at yahoo dot com
Description:
------------
I don't know if this is a php5 or php4 bug, but I'm sure something's wrong here.
Whenever  in php5 I do an array_count_values() on an array that contains numeric values as strings the result array uses string keys instead of the numeric values as indexes. This doesn't happen in php4.
I don't know what behaivor is correct.
I'm using the latest cvs versions of both php4 and php5 btw.

Reproduce code:
---------------
<?php
$books = Array('10', '3', '6', '10');
$quantities = array_count_values($books);
var_dump($books);
var_dump($quantities);
?>

Expected result:
----------------
This is what happens in php4:

array(4) {
  [0]=>
  string(2) "10"
  [1]=>
  string(1) "3"
  [2]=>
  string(1) "6"
  [3]=>
  string(2) "10"
}
array(3) {
  [10]=>
  int(2)
  [3]=>
  int(1)
  [6]=>
  int(1)
}

Actual result:
--------------
This is what happens if php5:

array(4) {
  [0]=>
  string(2) "10"
  [1]=>
  string(1) "3"
  [2]=>
  string(1) "6"
  [3]=>
  string(2) "10"
}
array(3) {
  ["10"]=>
  int(2)
  ["3"]=>
  int(1)
  ["6"]=>
  int(1)
}

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-06-18 14:24 UTC] programmer at bardware dot de
I noticed this problem with PHP5 RC2 on a Win2k box as an Apache2-module. I have some HMTL-Checkboxes and on the server side want to check the selected values. My checkboxes are equally named name="choice2[]" what lets PHP generate an array
$_POST["choice2"][0]
$_POST["choice2"][1]
$_POST["choice2"][2] etc. according to the selected values.
Each checkbox has it's unique value attribute.

To test for a certain value I "reverse" this array with
$arrTmp=array_count_values($_POST["choice2"]);

I now want to access $arrTmp[1] to check if the checkbox with the attribute value="1" was selected. The respective value was - if selected - 1, otherwise it's not existent in the $_POST["choice2"]-Array. This did not work. $arrTmp["1"] didn't work either. It was no way possible to access a member of the array.

The other poster mentioned the indexes are generated as strings, I want to point they cannot be accessed at all.

It worked best on PHP 4.3.6

Best,
Bernhard
 [2004-06-21 11:31 UTC] vladb at pseudo-infinity dot ro
The php5 result seems more accurate than the php4 one. You got strings in the $books array, no?

Changing this in php4 may break some code out there so maybe it'd be best to just mention it as a 4-5 inconsistency.
 [2004-06-21 17:50 UTC] ofirin at yahoo dot com
Ok, I think you're right, we all should be looking forward working with php5, rather than trying to fix old bugs in php4.
 [2004-08-04 16:39 UTC] alexis dot bosson at medias dot cnes dot fr
As said in :
- bug #9307
- bug #21918
- bug #22201
- and documentation of arrays : http://www.php.net/manual/en/language.types.array.php

" 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"). "

So the array_count_values must cast the integers contained in strings to integers.

Actually, with standard PHP behavior, it's impossible to access directly to those array elements which keys are strings containing integers, as said in comment of June 18
 [2004-08-24 07:43 UTC] php_bugs at michaeldouma dot com
Why is this bug closed? I am now experiencing the same 
problem with 5.0.1. The array from array_count_values 
appears normal with vardump, but there is no way to 
access the elements; neither as arrayname["2"] nor 
arrayname[2]. This worked fine with 4.3.x. 

vardump of result from array_count_values

array(5) {
  ["3"]=>
  int(2)
  ["2"]=>
  int(1)
  ["1"]=>
  int(1)
  ["0"]=>
  int(1)
  [0]=>
  int(0)
}

It looks like you'd be able to access the value with 
arrayname["2"], but you can't!
 [2004-08-24 15:55 UTC] ofirin at yahoo dot com
I'm opening back this bug again because I think the last user's submition needs revision.
 [2004-10-23 20:23 UTC] hholzgra@php.net
Thank you for your bug report. This issue has already been fixed
in the latest released version of PHP, which you can download at 
http://www.php.net/downloads.php

Fixed in 5.0.2
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 26 15:01:56 2024 UTC