如何根据内部内容获取数组的总值

发布于 2024-11-16 23:37:10 字数 900 浏览 2 评论 0原文

这是我的数组,

[Computers] => Array
(
   [0] => Array
       (
           [product_id] => 78
           [price] => 999.99
           [quantity] => 2
       )

   [1] => Array
       (
           [product_id] => 70
           [price] => 49.99
           [quantity] => 6
       )

)


[Scanners] => Array
(
   [0] => Array
       (
           [product_id] => 65
           [price] => 99.99
           [quantity] => 7
       )
   [1] => Array
       (
           [product_id] => 70
           [price] => 149.99
           [quantity] => 5
       )
   [2] => Array
       (
           [product_id] => 70
           [price] => 10.99
           [quantity] => 1
       )

)

我正在寻找一种方法根据内部数组来汇总外部数组的值

,例如 Computers 数组有两种产品,我需要根据数量获取总计。例如,计算机数组的 (999.99 *2) + (49.99 * 6) = 2299.92 等等......

here is my array

[Computers] => Array
(
   [0] => Array
       (
           [product_id] => 78
           [price] => 999.99
           [quantity] => 2
       )

   [1] => Array
       (
           [product_id] => 70
           [price] => 49.99
           [quantity] => 6
       )

)


[Scanners] => Array
(
   [0] => Array
       (
           [product_id] => 65
           [price] => 99.99
           [quantity] => 7
       )
   [1] => Array
       (
           [product_id] => 70
           [price] => 149.99
           [quantity] => 5
       )
   [2] => Array
       (
           [product_id] => 70
           [price] => 10.99
           [quantity] => 1
       )

)

I was looking for a way to total up the values of the outer arrays based on the inner arrays

so for example the Computers array has two products and i need to get the totals based on quantity. So for example (999.99 *2) + (49.99 * 6) = 2299.92 for the computer array and so on...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

别挽留 2024-11-23 23:37:10

这是一个未经测试的解决方案,但我不明白为什么它不起作用

$sum = 0;
foreach($array as $key => $value) {
    $sum += $value['quantity'] * $value['price'];
}

Here is a solution which is untested, but I don´t see why it shouldn´t work

$sum = 0;
foreach($array as $key => $value) {
    $sum += $value['quantity'] * $value['price'];
}
陈年往事 2024-11-23 23:37:10

这是一种替代方法,将产品映射到小计,然后对外部数组中每个项目的小计求和。

$totals = array_map(function($group){
    return array_sum(array_map(function($product) {
        return $product['price'] * $product['quantity'];
    }, $group));
}, $array);

var_dump($totals, array_sum($totals));

Here is an alternative approach which maps the products to sub-totals, then sums those sub-total for each of the items in the outer array.

$totals = array_map(function($group){
    return array_sum(array_map(function($product) {
        return $product['price'] * $product['quantity'];
    }, $group));
}, $array);

var_dump($totals, array_sum($totals));
请持续率性 2024-11-23 23:37:10

试试这个代码:

$sum=0;
foreach ($computers as $pc) {
   $sum += ($pc["price"] * $pc["quantity"]);
}

Try this code:

$sum=0;
foreach ($computers as $pc) {
   $sum += ($pc["price"] * $pc["quantity"]);
}
人疚 2024-11-23 23:37:10
  $total = 0;
  for( $i = 0; $i < count( Computers ); $i++ ){
       $total += ( Computers[$i]['price'] * Computers[$i]['quantity'] );
  }
  $total = 0;
  for( $i = 0; $i < count( Computers ); $i++ ){
       $total += ( Computers[$i]['price'] * Computers[$i]['quantity'] );
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文