There is addition and substation of qty calculation i m looking for in PHP How can i iterate through loop as below array 2021-10-27 above all date array you can see buy qty and sell qty is same so i want to remove those array. and in 2021-10-27 i want to return only buy_qty and buy_price.
Can some one help me with this array
Array
(
[TCS] => Array
(
[2020-04-30] => Array
(
[buy_qty] => 84
[buy_price] => 111368.5
)
[2020-05-04] => Array
(
[buy_qty] => 50
[buy_price] => 64178.7
)
[2020-05-18] => Array
(
[buy_qty] => 10
[buy_price] => 12636
)
[2020-06-25] => Array
(
[buy_qty] => 5
[buy_price] => 7054.75
)
[2020-06-26] => Array
(
[buy_qty] => 60
[buy_price] => 82527.05
)
[2020-06-29] => Array
(
[buy_qty] => 14
[buy_price] => 18979.45
)
[2020-06-30] => Array
(
[buy_qty] => 28
[buy_price] => 38424.9
)
[2020-07-07] => Array
(
[buy_qty] => 17
[buy_price] => 23889.9
)
[2020-07-13] => Array
(
[buy_qty] => 4
[buy_price] => 5563.8
)
[2020-07-16] => Array
(
[buy_qty] => 2
[buy_price] => 2685
)
[2020-07-24] => Array
(
[buy_qty] => 10
[buy_price] => 13580
)
[2020-07-27] => Array
(
[buy_qty] => 3
[buy_price] => 4038.6
)
[2020-08-07] => Array
(
[buy_qty] => 33
[buy_price] => 44011.95
)
[2020-08-10] => Array
(
[buy_qty] => 107
[buy_price] => 142097.2
)
[2020-08-11] => Array
(
[buy_qty] => 10
[buy_price] => 13180.2
)
[2020-08-14] => Array
(
[buy_qty] => 1
[buy_price] => 1356
)
[2020-08-20] => Array
(
[buy_qty] => 10
[buy_price] => 13520
)
[2020-11-19] => Array
(
[sell_qty] => 100
[sell_price] => 137574.35
)
[2020-11-20] => Array
(
[sell_qty] => 198
[sell_price] => 273883.2
)
[2020-11-24] => Array
(
[sell_qty] => 50
[sell_price] => 68513
)
[2020-12-04] => Array
(
[sell_qty] => 10
[sell_price] => 15639.5
)
[2021-01-06] => Array
(
[sell_qty] => 20
[sell_price] => 29887.9
)
[2021-01-07] => Array
(
[sell_qty] => 10
[sell_price] => 14941.3
)
[2021-02-12] => Array
(
[sell_qty] => 8
[sell_price] => 13825.4
)
[2021-07-29] => Array
(
[sell_qty] => 2
[sell_price] => 4652.7
)
[2021-10-27] => Array
(
[sell_qty] => 50
[sell_price] => 67600
[buy_qty] => 250
[buy_price] => 66450
)
)
)
output should be return Array ( [qty] => 250 [price] => 66450 )
Please help me
CodePudding user response:
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
.
CodePudding user response:
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
If you need the last result you can do it like:
$last = end($array['TCS']);
print_r(array(
"qty" => $last['buy_qty'],
"price" => $last['buy_price'],
));
This prints:
Array
(
[qty] => 250
[price] => -22677355.9
)
CodePudding user response:
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.