循环通过数组求一个元素的和

时间:2018-10-25 16:31:47

标签: php arrays

我有一个购物车,上面有三个物品:

$cart = [

    1031 => [
        'id'    => '1031',
        'model' => 'tr16',
        'price' => 100,
        'discount_applied' => '',
        'promo_name' => '',
        'promo_id' => ''
    ],
    1032 => [
        'id'    => '1032',
        'model' => 'tr16g',
        'price' => 100,
        'discount_applied' => '',
        'promo_name' => '',
        'promo_id' => ''
    ],
    1034 => [
        'id'    => '1034',
        'model' => 'tr17g',
        'price' => 100,
        'discount_applied' => '',
        'promo_name' => '',
        'promo_id' => ''
    ]
];

我有一个ID数组,代表可以享受折扣的物品,如下所示:

$itemIds = [

    0 => [
        0 => 1031
    ],
    1 => [
        0 => 1032
    ]
];

我遍历每个数组,并通过应用20%的折扣来更改$cart中的ID与$itemIds中的ID匹配的价格,并添加新的元素。该代码如下所示:

foreach($cart as &$item) {
    foreach($itemIds as $ids) {
        foreach($ids as $key => $value) { 

            if ($item['id'] == $value)   
            {             
                $item['discount_applied'] = $item['price'] * 0.2;
                $item['price'] = $item['price'] * .80;
                $item['promo_name'] = 'Test promo';
                $item['promo_id'] = 36;
            }     
        }
    }
}

在循环之前和之后将购物车打印到屏幕上,表明其工作正常。

但是,当我尝试遍历经过修改的$cart并计算单个折扣的总和时,我遇到了一个问题。

我的循环如下:

$cart['total_discount'] = 0;
foreach($cart as $item) 
{
    $cart['total_discount'] += $item['discount_applied'];
}
echo 'Total discount:' . $cart['total_discount'];

我希望看到折扣总和= 40,而是=60。在循环之前和之后将购物车打印到屏幕上,显示discount_applied中的项目1031和1032的值为20,并且该项目1034没有价值。

我们会帮助您识别我的错误所在。

如果要复制/粘贴,这里是所有代码。

$cart = [

    1031 => [
        'id'    => '1031',
        'model' => 'tr16',
        'price' => 100,
        'discount_applied' => '',
        'promo_name' => '',
        'promo_id' => ''
    ],
    1032 => [
        'id'    => '1032',
        'model' => 'tr16g',
        'price' => 100,
        'discount_applied' => '',
        'promo_name' => '',
        'promo_id' => ''
    ],
    1034 => [
        'id'    => '1034',
        'model' => 'tr17g',
        'price' => 100,
        'discount_applied' => '',
        'promo_name' => '',
        'promo_id' => ''
    ]
];

$itemIds = [

    0 => [
        0 => 1031
    ],
    1 => [
        0 => 1032
    ]
];  

echo '<h2>Cart BEFORE discount</h2>'; echo '<pre>';print_r($cart); echo '</pre>';


foreach($cart as &$item) {
    foreach($itemIds as $ids) {
        foreach($ids as $key => $value) { 

            if ($item['id'] == $value)   
            {             
                $item['discount_applied'] = $item['price'] * 0.2);
                $item['price'] = $item['price'] * .80;
                $item['promo_name'] = 'Test promo';
                $item['promo_id'] = 36;
            }     
        }
    }
}

echo '<h2>Cart AFTER discount</h2>'; echo '<pre>';print_r($cart); echo '</pre>';

$cart['total_discount'] = 0;
foreach($cart as $item) 
{
    echo $item['discount_applied'] . '<br>';
    $cart['total_discount'] += $item['discount_applied'];
}
echo 'Total discount:' . $cart['total_discount'];

1 个答案:

答案 0 :(得分:1)

您在初始循环中使用&$item也需要在最终循环中使用。您将折扣总额相加的循环。您还需要查看折扣值是否为数字,因为在您发布的代码中,购物车中的第三个项目的折扣值将为空,当尝试将其添加到商品时会抛出非数字错误总折扣。

您的最终循环已修改为可以正常工作:

$cart['total_discount'] = 0;
foreach($cart as &$item) {
    echo '*' . $item['discount_applied'] . '*<br />';  // just to show what is or isn't there
    $cart['total_discount'] += (is_numeric($item['discount_applied']) ? $item['discount_applied'] : 0);
}
echo 'Total discount:' . $cart['total_discount'];