Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 对循环中的数组元素进行计数_Php - Fatal编程技术网

Php 对循环中的数组元素进行计数

Php 对循环中的数组元素进行计数,php,Php,我只需要一点帮助 php 7.2 debian 9 apache2.4 我有数组2d,首先是这个吗 好的,我的问题不清楚 我给你介绍了我的阵列的完整结构 array[result] array[allproduct] array [1]=> [product] name product [quantity] 25 [price] 1.25 usd ar

我只需要一点帮助 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打印数据,但我不能计数 丁腈橡胶产品总数量所有产品平均价格和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():参数必须是数组或实现可计数的对象
谢谢你的帮助

我想这会有用的:

$counts = [];
$counts = array();
foreach($data["results"]["allproduct"]  as $key=> $result)
{
   $counts[$result['$key']][] = $result['price'];   
}

foreach ( $counts as $key => $item ) 
{
  $counts[$key] = array_sum( $item );
}   

您的问题不清楚,请立即更新它,但您需要
foreach($data[“results”][“allproduct”]]
,我看不到任何
$data[“results”][“allproduct”]
在您的示例中?您是否可以编辑您的问题并添加带有php格式数据的
数组
?并且输出不清楚,您希望对所有
数量
求和,并根据每个产品价格计算
平均价格
?因此,如果您有
数量=5/价格=1,数量=5/价格=2
,您需要
>总量=10,平均价格=((5*1)+(5*2))/total quantity=(5+10)/10=15/10=1.5
?你认为这有什么用?一个好的答案总是会解释做了什么以及为什么这样做,不仅是为了OP,而且是为了未来的访客。