简体   繁体   English

如何在php中的单个数组中添加值

[英]how to add to value in a single array in php

please someone will guide me to how to add the values of below conditions and then update the added value in code . 请有人指导我如何添加以下条件的值,然后更新代码中的添加值。

how to sum value of where code =low_order_fee and where code= goods_total then update the sum value in where code=goods_total. 如何对代码= low_order_fee和代码= goods_total的值求和,然后更新代码= goods_total的求和值。

  Array
    (
     [0] => Array
       (
        [order_total_id] => 999
        [order_id] => 194
        [code] => goods_total
        [title] => Goods-Total
        [text] => £130.00
        [value] => 130.0000
        [sort_order] => 1
    )

[1] => Array
    (
        [order_total_id] => 1000
        [order_id] => 194
        [code] => low_order_fee
        [title] => * Carriage
        [text] => £10.00
        [value] => 10.0000
        [sort_order] => 3
    )

[2] => Array
    (
        [order_total_id] => 1001
        [order_id] => 194
        [code] => sub_total
        [title] => Sub-Total
        [text] => £130.00
        [value] => 130
        [sort_order] => 4
    )

[3] => Array
    (
        [order_total_id] => 1002
        [order_id] => 194
        [code] => tax
        [title] => VAT (20%)
        [text] => £26.00
        [value] => 26.0000
        [sort_order] => 5
    )

[4] => Array
    (
        [order_total_id] => 1003
        [order_id] => 194
        [code] => total
        [title] => Invoice Total
        [text] => £166.00
        [value] => 166.0000
        [sort_order] => 9
    )

)

A rudimentary solution: 一个基本的解决方案:

foreach ($array_var as $key => $item) {
    if ($item['code'] == 'low_order_fee') {
        $first_val = $item['value'];
    }
    if ($item['goods_total'] == 'low_order_fee') {
        $sec_val = $item['value'];
        $position = $key;
    }
}
$array_var[$position]['goods_total'] = $first_val + $sec_val;

But maybe you should think about store the values in another way to make easier access to them. 但是也许您应该考虑以其他方式存储值,以便更轻松地访问它们。

I hope it helps. 希望对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM