From: v1d4l0k4 at gmail dot com
Operating system: Ubuntu 9.10
PHP version: 5.2.11
PHP Bug Type: Arrays related
Bug description: Inconsistent behavior in array_rand() after PHP 5.2.10 changes
Description:
------------
PHP 5.2.10 introduced a controversial BC break in array_rand() function
that doesn't sort array values when the $num_req param is equal to array
count.
This was the most incomprehensive change I've seen in the PHP development
so far.
If I have a random $num_req between 1 and array count I'll need to check
if I've to use array_rand() ou shuffle()? It seems so stupid...
On the other hand, if this change will remain it need to be fixed anyway:
there's no sense in allow array count in $num_req - it returns the array
unchanged with no warnings. In this case this parameter should receive a
value less than the array count. And it should be nice to have shuffle()
use recommendation in the warning if $num_req is equal to array count.
BTW, the manual notes about this array_rand() change is very very very
incomprehensive.
Reproduce code:
---------------
<?php
$array = range('a', 'z');
foreach (array_rand($array, count($array)) as $key) {
print $array[$key];
}
?>
Expected result:
----------------
MAYBE: frkzmnvuyiptdcsewbxgjqhloa
Actual result:
--------------
ALWAYS: abcdefghijklmnopqrstuvwxyz
--
Edit bug report at http://bugs.php.net/?id=50421&edit=1
--
Try a snapshot (PHP 5.2):
http://bugs.php.net/fix.php?id=50421&r=trysnapshot52
Try a snapshot (PHP 5.3):
http://bugs.php.net/fix.php?id=50421&r=trysnapshot53
Try a snapshot (PHP 6.0):
http://bugs.php.net/fix.php?id=50421&r=trysnapshot60
Fixed in SVN:
http://bugs.php.net/fix.php?id=50421&r=fixed
Fixed in SVN and need be documented:
http://bugs.php.net/fix.php?id=50421&r=needdocs
Fixed in release:
http://bugs.php.net/fix.php?id=50421&r=alreadyfixed
Need backtrace:
http://bugs.php.net/fix.php?id=50421&r=needtrace
Need Reproduce Script:
http://bugs.php.net/fix.php?id=50421&r=needscript
Try newer version:
http://bugs.php.net/fix.php?id=50421&r=oldversion
Not developer issue:
http://bugs.php.net/fix.php?id=50421&r=support
Expected behavior:
http://bugs.php.net/fix.php?id=50421&r=notwrong
Not enough info:
http://bugs.php.net/fix.php?id=50421&r=notenoughinfo
Submitted twice:
http://bugs.php.net/fix.php?id=50421&r=submittedtwice
register_globals:
http://bugs.php.net/fix.php?id=50421&r=globals
PHP 4 support discontinued: http://bugs.php.net/fix.php?id=50421&r=php4
Daylight Savings: http://bugs.php.net/fix.php?id=50421&r=dst
IIS Stability:
http://bugs.php.net/fix.php?id=50421&r=isapi
Install GNU Sed:
http://bugs.php.net/fix.php?id=50421&r=gnused
Floating point limitations:
http://bugs.php.net/fix.php?id=50421&r=float
No Zend Extensions:
http://bugs.php.net/fix.php?id=50421&r=nozend
MySQL Configuration Error:
http://bugs.php.net/fix.php?id=50421&r=mysqlcfg