将具有相同键值的数组数量相加

时间:2019-03-20 08:17:55

标签: arrays key add quantities

我正在尝试从不同的仓库中获取全部产品数量。 数组:

{
  "stocks": {
    "list": [
      {
        "products": [
          {
            "measuringUnit": "unit",
            "productCode": "code1",
            "productName": "name",
            "quantity": "4"
          },
        ],
        "warehouse": {
          "warehouseName": "warehouse1",
          "warehouseType": "en detail"
        }
      },
      {
        "products": [
          {
            "measuringUnit": "unit",
            "productCode": "code1",
            "productName": "name",
            "quantity": "10"
          },
        ],
        "warehouse": {
          "warehouseName": "warehouse2",
          "warehouseType": "en detail"
        }
      }
    ]
  }
}

我正在使用以下内容在显示屏上打印:

foreach($list as $item) {
    foreach($item['products'] as $product) {
        $wName = $item['warehouse']['warehouseName'];
        $wType = $item['warehouse']['warehouseType'];
        $pr =implode(',',$product);
        printf("%s,%s,%s".PHP_EOL,$wName,$wType,$pr);
    }
}

现在它显示以下内容:

warehouse1, en detail, name, code1, unit, 4, 
warehouse2, en detail, name, code1, unit, 10,  

我需要从不同的仓库中添加具有相同产品代码的产品的数量,例如:

name, code1, unit, 14,

尝试但失败了,帮助

0 个答案:

没有答案