$summary=$query->result_array(); //where the original array is created
print_r($summary); //dump contents
产生这个:
Array ( [0] => Array ( [RecordID] => 2 [UserID] => 3 [BookID] => 1 [Title] => FirstBook ) [1] => Array ( [RecordID] => 3 [UserID] => 3 [BookID] => 2 [Title] => Sequel ) )
我现在想用price元素填充多维数组,以便创建
的结果Array ( [0] => Array ( [RecordID] => 2 [UserID] => 3 [BookID] => 1 [Title] => FirstBook [Price] => 99 ) [1] => Array ( [RecordID] => 3 [UserID] => 3 [BookID] => 2 [Title] => Sequel [Price] => 99) )
我能想到的唯一方法是将多维数组分解为一维数组,修改它们,然后重新组装它们。虽然声音效率不高。有什么建议?
答案 0 :(得分:1)
您可以通过引用更新内部数组,请注意&
此处:
foreach($summary as &$details){
$details['Price'] = $price; // wherever $price comes from...
}
答案 1 :(得分:0)
尝试使用:
foreach ($summary as $idx => &$arrValue)
$arrValue['Price'] = ###;
答案 2 :(得分:0)
如果您已修复维度,请使用引用进行迭代并修改$ summary ...
<?php
foreach ($summary as &item) {
$item['price'] = 99;
}
如果您对参考文献有特别的异议/问题:
<?php
foreach ($summary as $key=>item) {
$summary[$key]['price'] = 99;
}