#29808 [NEW]: array_count_values bungles strings which are numbers
From: php_bugs at michaeldouma dot com Operating system: MacOSX or Linux PHP version: 5.0.1 PHP Bug Type: Arrays related Bug description: array_count_values bungles strings which are numbers Description: Similar to BugĀ #28435 which should not be closed. Can not access results from array_count_values when counting strings which happen to be numbers. Reproduce code: --- $items1 = array (1,2,2,3,3,3); $hist1 = array_count_values ($items1); print \nworks: .join(,, $hist1); print \nworks: .$hist1[1].,.$hist1[2].,.$hist1[3]; print \nworks: .$hist1[1].,.$hist1[2].,.$hist1[3]; $items2 = array (1,2,2,3,3,3,z,z); $hist2 = array_count_values ($items2); print \nworks: .join(,, $hist2); print \nfails: .$hist2[1].,.$hist2[2].,.$hist2[3]; print \nfails: .$hist2[1].,.$hist2[2].,.$hist2[3].,.$hist2[z]; print \n\n; var_dump($hist1); var_dump($hist2); Expected result: works: 1,2,3 works: 1,2,3 works: 1,2,3 works: 1,2,3,2 fails: ,, fails: ,,,2 Actual result: -- works: 1,2,3 works: 1,2,3 works: 1,2,3 works: 1,2,3,2 fails: 1,2,3 fails: 1,2,3,2 -- Edit bug report at http://bugs.php.net/?id=29808edit=1 -- Try a CVS snapshot (php4): http://bugs.php.net/fix.php?id=29808r=trysnapshot4 Try a CVS snapshot (php5.0): http://bugs.php.net/fix.php?id=29808r=trysnapshot50 Try a CVS snapshot (php5.1): http://bugs.php.net/fix.php?id=29808r=trysnapshot51 Fixed in CVS:http://bugs.php.net/fix.php?id=29808r=fixedcvs Fixed in release:http://bugs.php.net/fix.php?id=29808r=alreadyfixed Need backtrace: http://bugs.php.net/fix.php?id=29808r=needtrace Need Reproduce Script: http://bugs.php.net/fix.php?id=29808r=needscript Try newer version: http://bugs.php.net/fix.php?id=29808r=oldversion Not developer issue: http://bugs.php.net/fix.php?id=29808r=support Expected behavior: http://bugs.php.net/fix.php?id=29808r=notwrong Not enough info: http://bugs.php.net/fix.php?id=29808r=notenoughinfo Submitted twice: http://bugs.php.net/fix.php?id=29808r=submittedtwice register_globals:http://bugs.php.net/fix.php?id=29808r=globals PHP 3 support discontinued: http://bugs.php.net/fix.php?id=29808r=php3 Daylight Savings:http://bugs.php.net/fix.php?id=29808r=dst IIS Stability: http://bugs.php.net/fix.php?id=29808r=isapi Install GNU Sed: http://bugs.php.net/fix.php?id=29808r=gnused Floating point limitations: http://bugs.php.net/fix.php?id=29808r=float
#29808 [Opn]: array_count_values bungles strings which are numbers
ID: 29808 User updated by: php_bugs at michaeldouma dot com Reported By: php_bugs at michaeldouma dot com Status: Open Bug Type: Arrays related Operating System: MacOSX or Linux PHP Version: 5.0.1 New Comment: whoops, swapped Expected and Actual result Previous Comments: [2004-08-24 08:01:09] php_bugs at michaeldouma dot com Description: Similar to BugĀ #28435 which should not be closed. Can not access results from array_count_values when counting strings which happen to be numbers. Reproduce code: --- $items1 = array (1,2,2,3,3,3); $hist1 = array_count_values ($items1); print \nworks: .join(,, $hist1); print \nworks: .$hist1[1].,.$hist1[2].,.$hist1[3]; print \nworks: .$hist1[1].,.$hist1[2].,.$hist1[3]; $items2 = array (1,2,2,3,3,3,z,z); $hist2 = array_count_values ($items2); print \nworks: .join(,, $hist2); print \nfails: .$hist2[1].,.$hist2[2].,.$hist2[3]; print \nfails: .$hist2[1].,.$hist2[2].,.$hist2[3].,.$hist2[z]; print \n\n; var_dump($hist1); var_dump($hist2); Expected result: works: 1,2,3 works: 1,2,3 works: 1,2,3 works: 1,2,3,2 fails: ,, fails: ,,,2 Actual result: -- works: 1,2,3 works: 1,2,3 works: 1,2,3 works: 1,2,3,2 fails: 1,2,3 fails: 1,2,3,2 -- Edit this bug report at http://bugs.php.net/?id=29808edit=1
#28435 [Com]: array_count_values() problem
ID: 28435 Comment by: php_bugs at michaeldouma dot com Reported By: ofirin at yahoo dot com Status: Closed Bug Type: Arrays related Operating System: Any PHP Version: 4CVS-2004-05-18 (stable) New Comment: 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! Previous Comments: [2004-08-04 16:39:13] 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-06-21 17:50:47] 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-06-21 11:31:59] 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-18 14:24:41] 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-05-18 19:04:43] 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) } -- Edit this bug report at http://bugs.php.net/?id=28435edit=1