计算循环中数组的元素

时间:2018-03-21 08:58:37

标签: php

我只需要帮助 php 7.2 debian 9 apache2.4  我有阵列2d首先是这个 好吧,我的问题不清楚 我把你的数组的完整结构

 array[result]
    array[allproduct]
    array [1]=>
               [product] name product
               [quantity] 25
               [price]  1.25 usd
               array [1.1] => sale by month
                             liste of 12 months
   array[2] =>
               [product] name product
               [quantity] 20
               [price]  2.00 usd
               array [2.2] => sale by month
                             liste of 12 months
   end array allproduct
   end array result

我在foreach中打印我的数据,但我无法计算 nbr product totlal数量所有产品平均价格和12个月平均值1.1平均值

foreach($data["results"]["allproduct"]  as $key=>$result){
// print all element in table
}

我以这种形式找到所有这些元素

0 product quantity price

我想计算我的表1产品和数量的总数 我不想在我的版本中使用表1.1,而只是平均值 所以我的最后一场比赛将是

0 product quantity price
1 product quantity price
end foreach
total product 2 total quantity 45 sales 3.45 average

例如 所有尝试发送此 count():参数必须是实现Countable的数组或对象 谢谢你的帮助

1 个答案:

答案 0 :(得分:0)

我认为这会奏效:

#include <iostream>
#include <cstdlib>
#define cimg_display 0
#include "CImg.h"

using namespace cimg_library;
using namespace std;

int main() {
   // Create 640x480 image
   CImg<unsigned char> image(640,480,1,3);

   // Fill with magenta
   cimg_forXY(image,x,y) {
      image(x,y,0,0)=255;
      image(x,y,0,1)=0;
      image(x,y,0,2)=255;
   }

   // Make some colours
   unsigned char cyan[]    = {0,   255, 255 };
   unsigned char black[]   = {0,   0,   0   };
   unsigned char yellow[]  = {255, 255, 0   };

   // Draw black text on cyan
   image.draw_text(30,60,"Black 64pt on cyan",black,cyan,1,64);

   // Draw yellow partially transparent text on black
   image.draw_text(80,200,"Yellow 32pt on black semi-transparent",yellow,black,0.5,32);

   // Save result image as NetPBM PNM - no libraries required
   image.save_pnm("result.pnm");
}