在 PHP 中创建关联数组的数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10218680/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
create array of associative arrays in PHP
提问by anna
I want to create an array of associative arrays in a while loop. In each itteration of the while loop I want to add a new element in the array. How I can do that? After that I want to pass this array in a foreach and print the data. I have this part of code for now but obviously something is wrong with that.
我想在一个 while 循环中创建一个关联数组的数组。在 while 循环的每次迭代中,我想在数组中添加一个新元素。我怎么能做到这一点?之后我想在 foreach 中传递这个数组并打印数据。我现在有这部分代码,但显然有问题。
while($row2 = mysql_fetch_array($result))
{
$myarray = array("id"=>$theid, "name"=>name($id), "text"=>$row2[text]);
}
回答by laltin
To add an element in the end of an array use []
Example:
要在数组末尾添加元素,请使用[]
示例:
$myarray[] = array("id"=>$theid, "name"=>name($id), "text"=>$row2[text]);
回答by hakre
Obviously, okay, first pick it apart so there's something to learn:
显然,好吧,先把它拆开,这样有一些东西要学习:
while($row2 = mysql_fetch_array($result))
{
...
}
This part look's okay, let's look inside the loop:
这部分看起来没问题,让我们看看循环内部:
$myarray = array("id"=>$theid, "name"=>name($id), "text"=>$row2[text]);
There are multiple points. Probably most important is, as that is inside a loop, you overwrite $myarrayin each iteration. You want to add to an array instead. Let's do this:
有多个点。可能最重要的是,因为这是在循环内,所以您$myarray在每次迭代中都会覆盖。您想改为添加到数组。我们开工吧:
$myarray = array(); # initialize the array first!
while($row2 = mysql_fetch_array($result))
{
$myarray[] = $row2; # add the row
}
After that you can output it to proof that it basically works:
之后,您可以输出它以证明它基本有效:
var_dump($myarray);
That shows you an array that contains all rows. You then only need to change your database query so that it only returns the fields you're interested in.
这会显示一个包含所有行的数组。然后,您只需更改数据库查询,使其仅返回您感兴趣的字段。
In case you can't do that with the database, you can manipulate the array as well:
如果您不能对数据库执行此操作,您也可以操作数组:
$myarray = array(); # initialize the array first!
while($row2 = mysql_fetch_array($result))
{
$myarray[] = array(
"id" => $theid,
"name" => name($id),
"text" => $row2['text']
);
}
var_dump($myarray);
Now the result should look like you want it. To output $myarray:
现在结果应该看起来像你想要的。输出$myarray:
foreach ($myarray as $number => $row)
{
echo '<div>Number ', $number, ':<dl>';
foreach ($row as $k => $v)
{
printf("<dt>%s</dt><dd>%s</dd>\n", $k, htmlspecialchars($v));
}
echo '</dl></div>'
}
回答by lafor
If you're trying to add to $myarray in each iteration, do it like this:
如果您尝试在每次迭代中添加到 $myarray,请执行以下操作:
$myarray[] = array("id"=>$theid, "name"=>name($id), "text"=>$row2[text]);
or like this:
或者像这样:
array_push($myarray, array("id"=>$theid, "name"=>name($id), "text"=>$row2[text]));
回答by dan-lee
Obviously your access to $row2looked wrong, so I assumed that here to be right
显然你的访问$row2看起来是错误的,所以我认为这里是正确的
$myarray = array();
while($row2 = mysql_fetch_array($result)) {
// append something to your array with square brackets []
$myarray[] = array("id"=> $row2['id'], "name" => $row2['name'], "text"=>$row2['text']);
// or to maker this even shorter you could do
$myarray[] = $row2; // ... because it has the same array key names
}
Then later when you want to read from it:
然后当你想从中读取时:
foreach($myarray as $val) {
echo $val['name'].' (ID: '.$val['id'].') wrote following text: '.$val['text'];
}

