dsg24156 2016-09-12 14: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 06: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条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部