[英]How to copy an associative array inside another associative array in PHP
Can anyone tell me how to store an array of associative arrays? 谁能告诉我如何存储关联数组的数组? I thought this code would work, but it's not. 我以为这段代码行得通,但事实并非如此。 I tried researching it but couldn't find an answer. 我尝试研究它,但是找不到答案。
foreach($rows as $row) {
// Some SQL...
if ($stmt->execute()) {
while ($row2 = $stmt->fetch(PDO::FETCH_ASSOC)) {
$row['NestedResults'][] = $row2;
}
}
}
foreach ($rows as $row) {
foreach ($row['NestedResults'] as $results) {
echo $results['Item'] . '<br>';
}
}
PS: I know I shouldn't loop SQL statements like that, but it's a special case. PS:我知道我不应该那样循环SQL语句,但这是一个特例。
在你第一次foreach
循环,你需要通过$row
通过引用变量,否则你只是修改从元素的副本$rows
存储在$row
:
foreach($rows as &$row)
do you think it is workable to put the second loop inside the first one as below? 您认为将第二个循环放在第一个循环内是否可行,如下所示?
$row['NestedResults'][] = $row2; foreach ($row['NestedResults'] as $results) { echo $results['Item'] . '<br>'; }
not 100% sure... 不能100%确定...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.