简体   繁体   English

将值分配给2d数组

[英]Assigning Values to a 2d array

I am trying to assign values from a db to a 2d array, but its only showing the last iterms. 我试图将值从db分配给2d数组,但它只显示最后的iterms。

Here is the code: 这是代码:

while($row = mysql_fetch_array($results)){
$MyData = array( array("Focus Area", $row["FocusArea"]),
               array("Finding Title", $row["FindingTitle"]),
               array("Finding Detail", $row["FindindDetail"]) 
             ); 

}//End While

What am I doing wrong please help. 我做错了什么,请帮忙。

$MyData[] = $row;

would be enough 就足够了

I'd also suggest to make a function, as getting an array from db is a very common routine. 我还建议创建一个函数,因为从db获取数组是一个非常常见的例程。
So, you'll be able to get your data in one line, 所以,你将能够将你的数据放在一行,

$myData = getRows("SELECT * FROM table");

You're declaring a new array each time the loop runs. 每次循环运行时,您都会声明一个新数组。 Declare it out of the while loop, and add the new values. 将其声明为while循环,并添加新值。

$MyData = array();
while($row = mysql_fetch_array($results)){
$MyData[] = array( array("Focus Area", $row["FocusArea"]),
               array("Finding Title", $row["FindingTitle"]),
               array("Finding Detail", $row["FindindDetail"]) 
             ); 

}//End While
$myData = array();
while($row = mysql_fetch_array($results)){
$MyData[] = array( array("Focus Area", $row["FocusArea"]),
               array("Finding Title", $row["FindingTitle"]),
               array("Finding Detail", $row["FindindDetail"]) 
             ); 

}//End While

this will do the trick 这将成功

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM