So I am trying to write a script where I have obtained data that has employee names and the amounts they are paid from a database. The information was retrieved in JSON format, which I think I decoded correctly and put into an associative array. The problem I am running into is figuring out how to loop through the array to obtain the total paid for each employee.
Here is my code so far starting at the section where I decoded the JSON file.
//decode JSON file
$payout_array = json_decode($info, true);
echo '<pre>' . print_r($payout_array, true) . '</pre>';
/*foreach($payout_array as $key=>$value)
{
if($key['Name'] == "Jane")
{
$jane_payout = array_sum($value['Amount']);
}
if($name == "Mark")
{
$mark_payout = sum($amount);
}
}*/
/*foreach ($payout_array as $key => $sub_array) {
foreach ($sub_array as $sub_key => $value) {
if($value['Name'] == "Jane")
{
$jane_payout = array_sum($value['Amount']);
}
if($value == "Mark")
{
$mark_payout =+ sum($value['Amount']);
}
}
}*/
//Proccess data to calculate the amount paid to each employee
/*$mark_payout = array_sum($payout_array->Amount);*/
echo "Jane $".number_format($jane_payout, 2);
echo "<br>";
echo "Mark $".number_format($mark_payout, 2);
?>
The result I am currently seeing in the browser is first section of array
On picture three you can see Jane and Mark show a total for $0.00. My goal is to have the $0.00 actually display the total that was paid to each employee from all the array entries. Once I am able to do that I will be encoding that new information into a JSON file and posting it to a client server. The end result that the client needs to see is the name of the employee and the total amount they have been paid.
I have tried various foreach loops and even left some commented out in the code but can't get it to access the elements correctly to perform the calculations. I am new to this and have very little experience with PHP so any help is very much appreciated!