将数组值从数组添加到数组值

时间:2015-03-02 20:24:56

标签: php arrays

$prices具有以下值

Array
(
    [0] => 824.2776
    [1] => 1586.5128
    [2] => 2277.8424
    [3] => 2902.698
    [4] => 3465.5112
    [5] => 3957.4188

)

我在$vArray中也有一些凭证代码,如下所示:

Array
(
    [voucher1] => 30
    [voucher2] => 40

)

我想创建减去百分比的价格。在这种情况下,30%和40%......只是不确定如何。

 foreach($prices as $price){
    foreach($vArray as $key => $value){
        $prices[] = ($value / $price) * 100;
    }
  }

上述foreach正在$prices

中创建这些附加数组项
[6] => 3.6395505591805
[7] => 1.8909396759988
[8] => 1.3170358054622
[9] => 1.0335212274925
[10] => 0.86567315090484
[11] => 0.75806988130748

1 个答案:

答案 0 :(得分:0)

$prices = [
    100,
    824.2776,
    1586.5128,
    2277.8424,
    2902.698,
    3465.5112,
    3957.4188
];

$vArray = [
    'voucher1' => 30,
    'voucher2' => 40,
];

foreach ($prices as $price) {
    foreach ($vArray as $voucherId => $percentage){
        $prices[] = $price * (1 - ($percentage / 100));
    }
}

print_r($prices);

输出:

Array
(
    [0] => 100
    [1] => 824.2776
    [2] => 1586.5128
    [3] => 2277.8424
    [4] => 2902.698
    [5] => 3465.5112
    [6] => 3957.4188
    [7] => 70
    [8] => 60
    [9] => 576.99432
    [10] => 494.56656
      ...
    [20] => 2374.45128
)