while数组 php,在while循环中创建2D PHP数组(Create a 2D PHP array within a while loop)

在while循环中创建2D PHP数组(Create a 2D PHP array within a while loop)

我有一个准备好的声明来从数据库中提取信息。 从数据库中提取了3个信息字段,并且行数未知。

我有一个'while'循环来定义从每一行获得的信息应该发生什么。 我想为3行信息创建一个数组,让每一行都有一个数组,然后为所有这些数组创建一个数组(即一个2D数组)。

到目前为止,这是我的代码:

$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;

}

一旦完成,我希望数组看起来像这样:

Array (

Array (

[blogTitle] => First title

[blogDate] => First date

[blogId] => First ID

)

Array (

[blogTitle] => Second title

[blogDate] => Second date

[blogId] => Second ID

)

)

有没有办法写这个? 我只需要内部数组的密钥对。

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.

I have a 'while' loop to define what should happen to the information gained from each row. 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 (i.e. a 2D array).

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.

原文:https://stackoverflow.com/questions/21615529

更新时间:2020-09-09 18:09

最满意答案

试试这段代码:

$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

}

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

}

2014-02-06

相关问答

像这样的东西: $sql = "..."

$result = mysql_query(...) ...;

$result_array = array();

while($row = mysql_fetch_assoc($result)) {

$result_array[] = $row;

}

那会给你: $result_array[0] = array('key1' => 'val1', 'key2' => 'val2', ...);

$result_array[1] = array(

...

无需使用[]访问列表。 外部for循环已经返回列表。 你可以这样做 for i in reversed(labirynth):

for j in reversed(i):

# do stuf...

There is no need to access the list with []. The outer for loop already returns the list. You can just do for i in reversed(labirynth):

...

你可以尝试下面的代码 var frames = document.getElementsByClassName('frame');

var myElements = [];

var animatedClasses = ["text", "splash"];

for (var i = 0; i < frames.length; i++) {

if(!myElements[i]) myElements[i] = Array(animatedClasses.length);

...

作为使用json_encode函数的JSON对象。 然后,您可以轻松地使用Javascript读取它,因为它是一个原生的JavaScript对象。 http://php.net/manual/en/function.json-encode.php json_encode($array);

JSON在JQuery中很容易解析,但对于纯JavaScript,请参阅此处: http://www.json.org/js.html As a JSON Object using the json_encod

...

请注意: >>> matrix = [[0] * 3] * 3

>>> [x for x in matrix]

[[0, 0, 0], [0, 0, 0], [0, 0, 0]]

>>> [id(x) for x in matrix]

[32484168, 32484168, 32484168]

>>>

三行但只有一个对象。 有关s * n操作,请参阅文档,尤其是注释2。 固定: >>> m2= [[0] * 3 for i in xrange(5)]

>>> [id(x) for x in m

...

你正在导入numpy ,所以你不妨使用它:) Numpy数组对于这种类型的使用效率更高,更通用* 问题是你没有以你期望的形状创建voltageArray ,你实际上是将它初始化为两个列表的列表,第一个内部列表的长度为25,第二个内部列表的长度为630.实际上想要一个有形状的二维数组(25,630)。 timeArray = np.zeros(625) # Array of 625 zeros

voltageArray = np.zeros((25, 630)) # Array of zer

...

对于二维数组,您可能希望通过GetLength(0)和GetLength(1)而不是使用Length来获取长度: dailyMenu [,] daysOfMonth = new dailyMenu[4,5];

for (int column = 0; column < daysOfMonth.GetLength(0); column++)

{

for (int row = 0; row < daysOfMonth.GetLength(1); row++)

...

试试这段代码: $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[

...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值