I have a response like this:
"data": [
{
"pay": "2010000",
},
{
"pay": "3010000",
},
{
"pay": "3920000",
},
]
foreach ($data as $data) {
$sum += $data['pay'];
array_push($newArr, array(
"finalResult" => '0',
)
);
}
I want to get (individual pay divided by sum of all pay) results and should push this value into array. I have a foreach loop where I'm trying to get the final result. But the final result array is showing incorrect data. Is there anything I'm missing?
I tried this with two foreach loops like this. But I'm getting wrong result
foreach ($newArr as $newArr_key => $newArr_value) {
if ($sum != 0) {
$finalResult = $data['pay']/ $sum;
if (isset($finalResult) && $finalResult != '') {
$newArr[$newArr_key]['finalResult'] = $finalResult;
}
} else {
$finalResult = 'Division by Zero';
}
}