Php 在数组的末尾推送一个元素
我有一个名为result的数组,我这样生成它Php 在数组的末尾推送一个元素,php,arrays,Php,Arrays,我有一个名为result的数组,我这样生成它 while something $result[] = $row; array_push($result,$row['fld_ConsumptionValue'] * $energy_type_unit_price) // this won't do endwhile $result数组中填充了数据库记录。我想做的是推另一个我上面计算的值。输出如下: result -> Arra
while something
$result[] = $row;
array_push($result,$row['fld_ConsumptionValue'] * $energy_type_unit_price) // this won't do
endwhile
$result数组中填充了数据库记录。我想做的是推另一个我上面计算的值。输出如下:
result -> Array[6]
[0] -> Array[34]
[1] -> Calculated Value For 1st Record
[2] -> Array[34]
[3] -> Calculated Value For 2nd Record
...
...
...
我想得到的是这个输出
result -> Array[6]
[0] -> Array[35]
[1] -> Array[35]
...
...
我做错了什么?你不需要这样做,而是用简单的方式来做:
$result[] = $row['fld_ConsumptionValue'] * $energy_type_unit_price;
上述操作将把值添加到数组的末尾。如果要使用数组数组[34]
,请添加该数组。不确定如何生成包含34项的数组。或者如果我做对了,你必须这样做:
$row [] = $row['fld_ConsumptionValue'] * $energy_type_unit_price
$result[] = $row;
上面将新的添加到$行
,您将所有35个添加到主$结果
就是这样:
while something
// add new key to `$row`
$row['someKey'] = $row['fld_ConsumptionValue'] * $energy_type_unit_price;
// add `$row` to `$result`
$result[] = $row;
endwhile
最简单的方法是
$array[]=$var代码>
在您的案例中,$result[]=$row['fld\u ConsumptionValue']*$energy\u type\u unit\u price代码>
有关详细信息,请参阅
希望这有帮助,呃,他已经这么做了,但对他的情况不起作用<代码>:)