php - 如何对值求和并将行添加到数组codeigniter
问题描述
模型中的mysql
public function dailymovedate($datee,$dateee)
{
header("Content-Type: application/json");
$this->db->select("dailymovement.id_order,dailymovement.item_code,currency.currency_name as totalamountbuy,dailymovement.totalamountsale,CONCAT(dailymovement.detials_daily,' qyt',ordersale.totalqyt) AS detials_daily ", false);
$this->db->from('dailymovement');
$this->db->join('ordersale', 'ordersale.idorder= dailymovement.id_order','left outer');
$this->db->join('currency', 'currency.currency_number = ordersale.currency_number','left outer');
$this->db->where('dailymovement.data_daily>=', $datee);
$this->db->where('dailymovement.data_daily<=', $dateee);
$this->db->where('dailymovement.totalamountsale!=' ,null,FALSE);
$this->db->order_by("dailymovement.id_order", "desc");
$query = $this->db->get();
return json_encode($query->result(),JSON_UNESCAPED_UNICODE);
}
数组 json
[{"id_order":"21","item_code":"SALE","totalamountbuy":"$","totalamountsale":"48790","detials_daily":" 153"},
{"id_order":"23","item_code":"SALE","totalamountbuy":"$","totalamountsale":"20790","detials_daily":" 5"}
{"id_order":"20","item_code":"SALE","totalamountbuy":"ERUO","totalamountsale":"22200","detials_daily":" 40"},
{"id_order":"19","item_code":"SALE","totalamountbuy":"TL","totalamountsale":"4500","detials_daily":" 45"}]
求和值并添加行示例 求和值 totalamountsale IF totalamountbuy == $
我想添加一个额外的字段并使它像`
{"item_code":"TOTAL","totalamountbuy":"$","totalamountsale":"69580","detials_daily":" 158"}
现在结果
[{"id_order":"21","item_code":"SALE","totalamountbuy":"$","totalamountsale":"48790","detials_daily":" 153"},
{"id_order":"23","item_code":"SALE","totalamountbuy":"$","totalamountsale":"20790","detials_daily":" 5"}
{"id_order":"20","item_code":"SALE","totalamountbuy":"ERUO","totalamountsale":"22200","detials_daily":" 40"},
{"id_order":"19","item_code":"SALE","totalamountbuy":"TL","totalamountsale":"4500","detials_daily":" 45"},
{"id_order":"0","item_code":"TOTAL","totalamountbuy":"$","totalamountsale":"69580","detials_daily":" 158"}]
解决方案
// for demonstration purposes, using json_decode to get an array matching your sample data
$querydata = json_decode('[{"id_order":"21","item_code":"SALE","totalamountbuy":"$","totalamountsale":"48790","detials_daily":" 153"},
{"id_order":"23","item_code":"SALE","totalamountbuy":"$","totalamountsale":"20790","detials_daily":" 5"},
{"id_order":"20","item_code":"SALE","totalamountbuy":"ERUO","totalamountsale":"22200","detials_daily":" 40"},
{"id_order":"19","item_code":"SALE","totalamountbuy":"TL","totalamountsale":"4500","detials_daily":" 45"}]');
// initialize total array with 0 as starting value for the fields to sum up
$total = [ 'id_order' => '0', 'item_code' => 'TOTAL', 'totalamountbuy' => '$',
'totalamountsale' => 0, 'detials_daily' => 0 ];
foreach($querydata as $row) {
if($row->totalamountbuy == '$') { // if $, add up values
$total['totalamountsale'] += $row->totalamountsale;
$total['detials_daily'] += $row->detials_daily;
}
}
// append total to existing array
$querydata[] = $total;
echo json_encode($querydata);
推荐阅读
- javascript - 有没有办法为多个对象添加事件侦听器而不必全部写出来?
- function - 如何在 Haskell 中重用一个函数来有效地“循环”
- ios - 在启动期间出现错误 127 iOS 移动项目
- sql - SQL Server 中“group by”子句中的“Case”
- python-3.x - 如何在不每次运行 __init__ 的情况下访问单独的类中的游标变量?
- javascript - 价格的递增和递减值
- java - Java 11 升级:java.lang.NoClassDefFoundError: org/omg/CORBA/portable/IDLEntity
- javascript - Javascript替换第一个字符
- yacc - Yacc 程序只接受大于 1 的整数
- database - 使用 IoTDB 时按结果集时间分组不符合预期