From: jestrada at webmedia dot com dot mx Operating system: Win PHP version: 5.0.4 PHP Bug Type: Arrays related Bug description: array_diff returns empty in some cases
Description: ------------ the default function array_diff returns empty records in some cases. i have two arrays 1) (1),(3),(7),(9) 2) (2),(3) it must return r) (1), (7), (9), but it returns (), (), (9) i made a similar function by my self, and i had the same problem, then i review the code, and i found a line, a simple counter: if ($l_existe == false){ <b>$arr_result[$i]</b> = $st_valorComparado; $i_result++; } the var $i must be $i_result, that's why the array contains in somecases, in which the number of found elements is less than the max elements, the empty recs, because $arr_result[$i] is putting the value of $st_valorComparado in another index higher than the max of elements found. Maybe the code of the original funcion have a similar error. Reproduce code: --------------- function fcn_array_diff($arr_comparado, $arr_compararEn){ $i_maxComparado = count($arr_comparado); $i_maxCompararEn = count($arr_compararEn); $arr_result[0] = ""; $i_result = 0; for ($i=0; $i<$i_maxComparado; $i++){ $st_valorComparado = chop($arr_comparado[$i]); $l_existe = false; for ($j=0; $j<$i_maxCompararEn; $j++){ $st_valorCompararEn = chop($arr_compararEn[$j]); if ($st_valorComparado == $st_valorCompararEn){ $l_existe = true; break; } } <b>if ($l_existe == false){ $arr_result[$i_result] = $st_valorComparado; $i_result++; }</b> } return $arr_result; } -- Edit bug report at http://bugs.php.net/?id=33887&edit=1 -- Try a CVS snapshot (php4): http://bugs.php.net/fix.php?id=33887&r=trysnapshot4 Try a CVS snapshot (php5.0): http://bugs.php.net/fix.php?id=33887&r=trysnapshot50 Try a CVS snapshot (php5.1): http://bugs.php.net/fix.php?id=33887&r=trysnapshot51 Fixed in CVS: http://bugs.php.net/fix.php?id=33887&r=fixedcvs Fixed in release: http://bugs.php.net/fix.php?id=33887&r=alreadyfixed Need backtrace: http://bugs.php.net/fix.php?id=33887&r=needtrace Need Reproduce Script: http://bugs.php.net/fix.php?id=33887&r=needscript Try newer version: http://bugs.php.net/fix.php?id=33887&r=oldversion Not developer issue: http://bugs.php.net/fix.php?id=33887&r=support Expected behavior: http://bugs.php.net/fix.php?id=33887&r=notwrong Not enough info: http://bugs.php.net/fix.php?id=33887&r=notenoughinfo Submitted twice: http://bugs.php.net/fix.php?id=33887&r=submittedtwice register_globals: http://bugs.php.net/fix.php?id=33887&r=globals PHP 3 support discontinued: http://bugs.php.net/fix.php?id=33887&r=php3 Daylight Savings: http://bugs.php.net/fix.php?id=33887&r=dst IIS Stability: http://bugs.php.net/fix.php?id=33887&r=isapi Install GNU Sed: http://bugs.php.net/fix.php?id=33887&r=gnused Floating point limitations: http://bugs.php.net/fix.php?id=33887&r=float No Zend Extensions: http://bugs.php.net/fix.php?id=33887&r=nozend MySQL Configuration Error: http://bugs.php.net/fix.php?id=33887&r=mysqlcfg