PHP 4.0 Bug #2460: Broken array_push? Or seriously misbehaving multidimensional arrays???

From: Date: Thu, 07 Oct 1999 03:48:24 +0000
Subject: PHP 4.0 Bug #2460: Broken array_push? Or seriously misbehaving multidimensional arrays???
Groups: php.dev 
Request: Send a blank email to php-dev+get-11593@lists.php.net to get a copy of this message
From: trevorl@discovermusic.com Operating system: Solaris 7 x86 PHP version: 4.0 Beta 2 PHP Bug Type: Other Bug description: Broken array_push? Or seriously misbehaving multidimensional arrays??? Okay, this is a really weird one. In my attempt to create a database mapping script I started playing with multidimensional arrays. Currently this script does not do any database access at all, and should be just plain old PHP. Here is an example array: $dbs = array( "dbone" => array( "tableone" => array("col1", "col2", "col3","col4"), "tabletwo" => array("col1", "col2", "col3","col4") ), "dbtwo" => array( "tableone" => array("col1", "col2", "col3","col4"), "tabletwo" => array("col1", "col2", "col3","col4") ) ); With this bit of code, I can walk my array: while (list($dbname,$table) = each($dbs)) { while (list($tablename,$column) = each($table)) { while ($columnname = each($column)) { print "$dbname contains $tablename which contains $columnname[1]<br>\n"; } } } reset($dbs); // Rewind... And it traverses the entire array, and produces the expected results: dbone contains tableone which contains col1 ... <trimmed> ... dbtwo contains tabletwo which contains col4 Now I want to add another database element to the array: $newdb = array( "dbthree" => array( "tableone" => array("col1", "col2", "col3","col4"), "tabletwo" => array("col1", "col2", "col3","col4") ) ); If I use array_push to add it to $dbs: $numpushed = array_push($dbs, $newdb); print "Pushed: $numpushed<br>\n"; It reports that it's pushed three elements onto the array, and re-running the script above that walks the array I get results that look like this: dbone contains tableone which contains col1 ... <trimmed again> ... dbtwo contains tabletwo which contains col4 0 contains dbthree which contains Array 0 contains dbthree which contains Array "manually" pushing the new element onto the array by using "$dbs[] = $newdb;" produces the same results, so I really don't think it's the array_push function. Maybe I'm making a programmatic mistake somewhere, but this is really not the behavior I expected. Below, I'm including the complete script that I've been using that creates this strange behavior. In this script, I also test my ability to modify sub elements of the array (this works just fine), as well as different ways of pulling values out of the arrays, which also work as expected. Anyway, here's the script... <?php $dbs = array( "dbone" => array( "tableone" => array( "col1", "col2", "col3", "col4" ), "tabletwo" => array( "col1", "col2", "col3", "col4" ) ), "dbtwo" => array( "tableone" => array( "col1", "col2", "col3", "col4" ), "tabletwo" => array( "col1", "col2", "col3", "col4" ) ) ); /* this creates an array containing "dbone" and "dbtwo" */ $foo = array_keys($dbs); /* this creates an array containing "tableone" and "tabletwo" from "dbone" */ $bar = array_keys($dbs[$foo[0]]); print "$foo[0]<br>\n"; // prints "dbone" print "$bar[0]<br>\n"; // prints "tableone" /* this returns an array of the values from "tableone" of "dbone" */ $baz = array_values($dbs[$foo[0]][$bar[0]]); print "$baz[0]\n<br>\n"; // prints "col1" print "" .$dbs[$foo[0]][$bar[0]][0] ."\n<br>\n"; // prints "col1" $gronk = $dbs[$foo[0]][$bar[0]][0]; // assigns "col1" to gronk print "$gronk\n<br>\n"; // prints "col1" while (list($dbname,$table) = each($dbs)) { while (list($tablename,$column) = each($table)) { while ($columnname = each($column)) { print "$dbname contains $tablename which contains $columnname[1]\n<br>\n"; } } } reset($dbs); // Rewind... $dbs[$foo[1]][$bar[1]][1] = "col69"; print "new: " .$dbs[$foo[1]][$bar[1]][1] ."\n<br>\n"; while (list($ndbname,$ntable) = each($dbs)) { while (list($ntablename,$ncolumn) = each($ntable)) { while ($ncolumnname = each($ncolumn)) { print "$ndbname contains $ntablename which contains $ncolumnname[1]\n<br>\n"; } } } ?> reset($dbs); // Rewind... $newdb = array( "dbthree" => array( "tableone" => array( "col1", "col2", "col3", "col4" ), "tabletwo" => array( "col1", "col2", "col3", "col4" ) ) ); /* $dbs[] = $newdb;*/ $numpushed = array_push($dbs, $newdb); print "Pushed: $numpushed\n<br>\n"; while (list($ndbname,$ntable) = each($dbs)) { while (list($ntablename,$ncolumn) = each($ntable)) { while ($ncolumnname = each($ncolumn)) { print "$ndbname contains $ntablename which contains $ncolumnname[1]\n<br>\n"; } } } reset($dbs); // Rewind...

« previous php.dev (#11593) next »