我想從數據庫動態地將新數組添加到現有數組,但我的循環只添加mysql_fetch_row的最後一行。我認爲它實際上是覆蓋相同的數組。在PHP foreach循環中動態添加數組
PHP代碼
<?php
$con = require_once('./dbconnect.php');
global $con;
mysql_select_db("packages", $con);
$packages = mysql_query("SHOW TABLES FROM packages");
while($row = mysql_fetch_row($packages)){
$node = array();
foreach($row as $key2 => $value2){
$node[$row[0]] = array("Item1" => "Other dynamic Info here");
}
}
print_r($node);
mysql_close($con);
?>
輸出如下:
Array
(
[Pack1] => Array
(
[Item1] => Other dynamic Info here
)
)
應該輸出:
Array
(
[Pack1] => Array
(
[Item1] => Other dynamic Info here
)
)
Array
(
[Pack2] => Array
(
[Item2] => Other dynamic Info here
)
)
我一直試圖讓這個的foreach( )循環現在工作大約一天...什麼我做錯了嗎?
那麼,你每次都在while循環中覆蓋$ node = array()。它將在時間結束後包含單個結果。 –