Bug #13679: When using array_unique with an array null values are created for the new array
| From: | msaladna at mediaone dot net | Date: | Mon, 15 Oct 2001 22:32:28 +0000 |
| Subject: | Bug #13679: When using array_unique with an array null values are created for the new array | ||
| Groups: | php.dev | ||
| Request: | Send a blank email to php-dev+get-67988@lists.php.net to get a copy of this message | ||
From: msaladna@mediaone.net
Operating system: Slackware 8.0
PHP version: 4.0.6
PHP Bug Type: Arrays related
Bug description: When using array_unique with an array null values are created for the new array
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 bug report at: http://bugs.php.net/?id=13679&edit=1