[英]Create a 2D PHP array within a while loop
I have a prepared statement to draw information from a database. 我有一条准备好的语句,可以从数据库中提取信息。 There are 3 fields of information drawn from the database and an unknown number of rows.
从数据库中提取了3个信息字段,行数未知。
I have a 'while' loop to define what should happen to the information gained from each row. 我有一个“ while”循环来定义从每一行获取的信息应该发生什么。 I would like to create an array for the 3 fields of information to sit in for each row, then an array for all of these arrays to sit in (ie a 2D array).
我想为每行中的3个信息字段创建一个数组,然后为所有要放置的所有数组创建一个数组(即2D数组)。
Here's my code so far: 到目前为止,这是我的代码:
$bArray = [];
$bList = $mysqli->prepare('SELECT blog_title,blog_date,blog_id FROM cms_blog');
$bList->execute();
$bList->bind_result($bListTitle,$bListDate,$bListId);
while ($bList->fetch()) {
$bArray['bTitle']=$bListTitle;
$bArray['bDate']=$bListDate;
$bArray['bId']=$bListId;
}
I would like the array to look something like this once complete: 我希望数组一旦完成就看起来像这样:
Array (
Array (
[blogTitle] => First title
[blogDate] => First date
[blogId] => First ID
)
Array (
[blogTitle] => Second title
[blogDate] => Second date
[blogId] => Second ID
)
)
Is there a way to write this? 有没有办法写这个? I only need key pairs for the inner arrays.
我只需要内部数组的密钥对。
Try this code: 试试这个代码:
$bArray = array();
$bList = $mysqli->prepare('SELECT blog_title,blog_date,blog_id FROM cms_blog');
$bList->execute();
$bList->bind_result($bListTitle,$bListDate,$bListId);
while ($bList->fetch()) {
$iArray=array(); // Inner array
$iArray['bTitle']=$bListTitle;
$iArray['bDate']=$bListDate;
$iArray['bId']=$bListId;
$bArray[] = $iArray; // Insert the inner array into $bArray
}
Maybe you need to use this: 也许您需要使用以下代码:
for ($row = 0; $row < 3; $row++)
{
for ($col = 0; $col < 3; $col++)
{
$var[$row][$col]="col: ".$col." Row: ".$row;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.