Bug #13679 Updated: When using array_unique with an array null values are created for the new array

From: Date: Mon, 15 Oct 2001 23:23:40 +0000
Subject: Bug #13679 Updated: When using array_unique with an array null values are created for the new array
References: 1  Groups: php.dev 
Request: Send a blank email to php-dev+get-67992@lists.php.net to get a copy of this message
ID: 13679 Updated by: sniper Reported By: msaladna@mediaone.net Old Status: Open Status: Feedback Bug Type: Arrays related Operating System: Slackware 8.0 PHP Version: 4.0.6 New Comment: status -> feedback Previous Comments: ------------------------------------------------------------------------ [2001-10-15 18:45:19] jeroen@php.net Can you please provide a short stand-alone script exhibiting the problems? Try to use only array(), array_unique() and var_dump() functions, and in any case no functions like mysql_query(). (there exists a 'null' keyword, for if you need it) ------------------------------------------------------------------------ [2001-10-15 18:32:27] msaladna@mediaone.net Creating a mysql database that holds a field of values, e.g. TLDs, with row 0 containing cx nu com org net, row 1 containing it at de us com, and so forth, a semi-annoying bug can be created with array_unique() that will insert null elements into the new array. For example: <? $connection = mysql_connect("localhost","classicgames","classicgames"); mysql_select_db("classicgames",$connection); $query = "select votes from tldvote"; $result = mysql_query($query); $i = 0; while($row = mysql_fetch_row($result)) { $string = implode("",$row); $tempArray = explode(" ",$string); for($j = 0; $j < 5; $j++) { $votes[$i][$j] = $tempArray[$j]; } $i++; } for($i = 0, $k = 0; $i < sizeOf($votes); $i++) for($j = 0; $j < 5; $j++) { $type[$k] = $votes[$i][$j]; $k++; } /* Bug is created here, just this script alone will produce an array of unique types as well as null (more-so towards "") types. */ $tempType = array_unique($type); for($i = 0; $i < sizeOf($type); $i++) for($j = 0; $j < sizeOf($tempType); $j++) if($type[$i] == $tempType[$j]) $tally[$j] = $tally[$j] + 1; array_multisort($tally, SORT_DESC,$tempType); for ($i = 0; $i < sizeOf($tally); $i++) echo $tempType[$i]." has ".$tally[$i]." votes.<br>"; ?> Now, I was able to fix this by adding: <? for($i = 0; $i < sizeOf($tempType); $i++) if($tempType[$i] == NULL) { array_push($tempType,$i); array_pop($tempType); } ?> which simply takes the new unique array and pushes null values to the top, then pops them out of the array. Configure line: './configure' '--disable-debug' '--with-config-file-path=/usr/local/apache/conf' '--with-mysql=/usr/local/mysql' '--prefix=/usr/local/php4' '--with-apxs=/usr/local/apache/bin/apxs' '--enable-track-vars' '--with-xml' '--enable-track-errors' '--enable-force-cgi-redirect' '--enable-discard-path' '--enable-safe-mode' '--enable-calendar' '--with-jpeg-dir=/usr/local' '--with-png-dir=/usr/local' '--with-gd=/usr' '--enable-gd-native-tt' '--with-zlib-dir=/usr/local' '--with-zlib' '--enable-trans-sid' '--enable-ftp' '--with-png-dir=/usr/local' '--with-gettext' '--enable-memory-limit' '--with-mcrypt' '--with-mcrypt-dir=/usr' '--with-pspell-dir=/usr/local/pspell' If any other information is required, just write an e-mail back. ------------------------------------------------------------------------ Edit this bug report at http://bugs.php.net/?id=13679&edit=1

« previous php.dev (#67992) next »