乘在PHP 2数组值 [英] multiply two array values in php

查看:124
本文介绍了乘在PHP 2数组值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将伴随着另外两个数组值用下面的公式:数量*价格=总每个多,那么所有的总和

I am trying to multiply two array values with each other with the following formula: Qty* Price = total of each multiple, then sum of all.

Array
(
[qty] => Array
    (
        [0] => .6
        [1] => .2
        [2] => .4
        [3] => .75
        [4] => 0.3
    )

[price] => Array
    (
        [0] => 1.2
        [1] => 0.5
        [2] => 0.8
        [3] => 12
        [4] => 2
    )

)

我无法弄清楚如何做到这一点。看到关于他们的固定值,即2或5等,但在我的情况下,每个数量都有不同的价格乘几个例子。请可有人建议如何做到这一点。

I could not figure out how to do that. Saw few example regarding the multiply in which they fixed value i.e. 2 or 5 etc. But in my case each qty has different price. Please can somebody suggest how to do that.

推荐答案

下面将只要两个数组的大小相同的工作。

The following will work as long as both arrays are the same size.

for($i = 0; $i < count($qty); $i++) {
    $result[] = $qty[$i] * $price[$i];
}

其结果将是在 $结果阵列。

要得到的总和:

echo "sum(result) = " . array_sum($result) . "\n";

这篇关于乘在PHP 2数组值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆