简体   繁体   中英

How can I find qty of given array

There is addition and substation of qty calculation im looking for in PHP How can i iterate through loop Example date 2022-07-01 & 2022-07-02 buy_qty should be add than 2022-07-03 sell_qty should be remove from buy_qty like buy_qty = 8 - 3 (sell_qty)....

Array
(
    [TCS] => Array
        (
            [2022-07-01] => Array
                (
                    [buy_qty] => 5
                    [buy_price] => 150
                )

            [2022-07-02] => Array
                (
                    [buy_qty] => 3
                    [buy_price] => 100
                )

            [2022-07-03] => Array
                (
                    [sell_qty] => 3
                    [sell_price] => 100
                )

            [2022-07-04] => Array
                (
                    [buy_qty] => 2
                    [buy_price] => 100
                    [sell_qty] => 4
                    [sell_price] => 100
                )

            [2022-07-05] => Array
                (
                    [sell_qty] => 2
                    [sell_price] => 100
                    [buy_qty] => 1
                    [buy_price] => 100
                )

            [2022-07-06] => Array
                (
                    [buy_qty] => 5
                    [buy_price] => 150
                    [sell_qty] => 2
                    [sell_price] => 100
                )

        )

)

output should be return ( [qty] => 5 )

Please help me

I think you're looking for this (this prints 5):

$qty = 0;

foreach ($array['TCS'] as $changes) {
    if (isset($changes['buy_qty'])) {
        $qty += $changes['buy_qty'];
    }
    if (isset($changes['sell_qty'])) {
        $qty -= $changes['sell_qty'];
    }
}

echo $qty;

Loop through the TCS array, and for each sub-array: if a buy_qty key is set, add that to $qty , and if a sell_qty key is set, subtract that from the $qty .

You can use array functions to achieve this without loops:

$buy_qty_sum = array_sum(array_column($array['TCS'], 'buy_qty'));

$sell_qty_sum = array_sum(array_column($array['TCS'], 'sell_qty'));

$result = $buy_qty_sum - $sell_qty_sum; // 5

iterate through the array twice as shown below do adding and subtracting of buy sell quantity and store in the finalValue variable which is initially declared 0

Refer here to know how isset() function works

`$data = Array
(
'TCS' => Array
    (
        '2022-07-01' => Array
            (
                'buy_qty' => 5,
                'buy_price' => 150
            ),

        '2022-07-02' => Array
            (
                'buy_qty' => 3,
                'buy_price' => 100
            ),

        '2022-07-03' => Array
            (
                'sell_qty' => 3,
                'sell_price' => 100
            ),

        '2022-07-04' => Array
            (
                'buy_qty' => 2,
                'buy_price' => 100,
                'sell_qty' => 4,
                'sell_price' => 100
            ),

        '2022-07-05' => Array
            (
                'sell_qty' => 2,
                'sell_price' => 100,
                'buy_qty' => 1,
                'buy_price' => 100
            ),

        '2022-07-06' => Array
            (
                'buy_qty' => 5,
                'buy_price' => 150,
                'sell_qty' => 2,
                'sell_price' => 100
            )

    )

);
$finalValue=0;
foreach($txt as $key => $value){

foreach($value as $k => $v){
    if(isset($v['buy_qty']))
        $finalValue += $v['buy_qty'];
    if(isset($v['sell_qty']))
        $finalValue -= $v['sell_qty'];
  }
}
echo $finalValue;`//to print the value of buy-sell after calculations

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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