dsg24156 2016-09-12 22:37
浏览 64
已采纳

PHP数组:分组,总和

I'm trying to print out shopping cart orders, however, instead of displaying all products from that order I want to group the orders by the order ID so that I can display the sum of the order and link to a detail page.

Instead of this

---------------------------------------
date        order number        total
---------------------------------------
today        1234WEDK             600
---------------------------------------
today        1234WEDK             500
---------------------------------------

I'd like to display this

---------------------------------------
date        order number        total
---------------------------------------
today        1234WEDK            1100
---------------------------------------

Here's a sample array

array:2 [▼
  0 => array:9 [▼
    "id" => 57
    "order_id" => 51
    "order_hash" => "1234WEDK"
    "price" => 600
  ]
  1 => array:9 [▼
    "id" => 58
    "order_id" => 51
    "order_hash" => "1234WEDK"
    "price" => 500
  ]
]
  • 写回答

2条回答 默认 最新

  • dongou2019 2016-09-13 14:56
    关注

    Assuming you are getting your data from a database, you should make it to do all the calculations for you

    SELECT order_hash, date, sum(price) as total FROM cart GROUP BY order_hash
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办