需要改变数组值

时间:2017-09-28 06:15:02

标签: php arrays

您好我正在使用下面的数组,并希望更改项目索引中的价格键的值请帮助。在数组中查看items数组和显示价格的位置我想在此添加自定义价格值,并需要再次使用带有所有关联键值的已修改数组

Array
    ([0] => Array(
          [customer_id] => 7
          [base_shipping_tax_amount] => 0.0000
          [shipping] => Array(
                        [entity_id] => 60
                        [customer_id] => 7
                    )        
                [items] => Array(
                        [0] => Array(
                                [sku] => bundlet01
                                [price] => 200.0000
                            )
                        [1] => Array(
                                [sku] => S12SX407
                                [price] => 0.0000
                                [parent_item] => Array
                                    (
                                        [sku] => bundlet01
                                    )
                            )
                        [2] => Array(
                                [sku] => S18SX001
                                [price] => 0.0000
                                [parent_item] => Array(
                                        [sku] => bundlet01
                                    )
                            )
                    )
            )
    )

2 个答案:

答案 0 :(得分:1)

让我们假设这个数组保存在$ carts变量中,然后

    foreach ($carts as $cart) {
        foreach ($cart['items'] as &$item) {
            $item['price'] = 1.00; // put you custom price what ever you want
        }
    }

答案 1 :(得分:0)

如果您想更改一个项目的价格,可以这样访问:

$itemIndex = 0; // or which item you need to change the price
$newPrice = 5.00;
$array[0]['items'][$itemIndex]['price'] = $newPrice;

$array是你的数组。