Cnory
Cnory
2017-08-28 09:15

【PHP】合并二维数组内指定的相同键并计算另一键的和后组成新的二位数组

1
  • 二维数组
  • 合并
  • php

array(4) {
  [0] => array(5) {
    ["id"] => int(1)
    ["order_num"] => string(11) "20170823-01"
    ["name"] => string(4) "4015"
    ["num"] => int(2000)
    ["time"] => int(1503590400)
  }
  [1] => array(5) {
    ["id"] => int(2)
    ["order_num"] => string(11) "20170823-02"
    ["name"] => string(4) "4015"
    ["num"] => int(3000)
    ["time"] => int(1503590400)
  }
  [2] => array(5) {
    ["id"] => int(3)
    ["order_num"] => string(11) "20170823-03"
    ["name"] => string(4) "4123"
    ["num"] => int(1000)
    ["time"] => int(1503590400)
  }
  [3] => array(5) {
    ["id"] => int(4)
    ["order_num"] => string(11) "20170823-04"
    ["name"] => string(4) "4123"
    ["num"] => int(1000)
    ["time"] => int(1503590400)
  }
}

原始数组如上
需要将 ["name"] 相同的合并到一起,且所对应的 ["num"]的值为原始数据中两个值的和
可能有多个相同["name"]
因为是新手,试了很多方法都没实现,想请教大牛们,应该怎么实现?
其他的键可以忽略

最终的结果是这种


array(4) {
  [0] => array(5) {
    ["name"] => string(4) "4015"
    ["num"] => int(5000)
        ["time"] => int(1503590400)
  }
  [1] => array(5) {
    ["name"] => string(4) "4123"
    ["num"] => int(2000)
    ["time"] => int(1503590400)
  }

}
  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

2条回答