我在gridview中有这个:
[
'value' => function ($data) {
$summ = 0;
$str = 0;
foreach($data->itemsOrder as $request) {
$str .= $request->order_items_quantity;
$summ += $str;
}
return $summ;
},
],
我得到类似的东西' 123'在列单元格中,但不是' 6'。我如何获得元素总和?
答案 0 :(得分:1)
试试这个..
gridview [
'value' => function ($data) {
$summ = 0;
$str = 0;
foreach($data->itemsOrder as $request) {
$summ +=$request->order_items_quantity;
}
return $summ;
},
],
答案 1 :(得分:1)
gridview [
'value' => function ($data) {
$summ = 0;
$str = 0;
foreach($data->itemsOrder as $request) {
//$str .= $request->order_items_quantity; //this means you are concating string each time which you dont need to do
$str = $request->order_items_quantity;
$summ += $str;
}
return $summ;
},
],
希望你在php
中得到.
的含义
答案 2 :(得分:1)
因为你是连接字符串而不是添加数字。
所以请替换$str .= $request->order_items_quantity;
与
$str += floatval($request->order_items_quantity);
或者order_items_quantity是一个整数
$str += intval($request->order_items_quantity);