php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #13679 When using array_unique with an array null values are created for the new array
Submitted: 2001-10-15 18:32 UTC Modified: 2001-11-09 14:58 UTC
From: msaladna at mediaone dot net Assigned:
Status: Closed Package: Arrays related
PHP Version: 4.0.6 OS: Slackware 8.0
Private report: No CVE-ID: None
 [2001-10-15 18:32 UTC] msaladna at mediaone dot 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.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2001-10-15 18:45 UTC] 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 19:23 UTC] sniper@php.net
status -> feedback
 [2001-11-09 14:58 UTC] sander@php.net
No feedback. Closing.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue May 07 22:01:30 2024 UTC