1

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

second section of array

errors and total

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!

masha
  • 11
  • 3

1 Answers1

1

Untested but this is generally what you're looking for.

<?php
$payout_array = json_decode($info, true);

echo '<pre>' . print_r($payout_array, true) . '</pre><br/><br/><br/><br/>';

foreach($payout_array['records'] as $key => $value)
{
    if (!array_key_exists('fields', $value)) {
        print 'Bad input data detected' . PHP_EOL;
    }

    $name = $value['fields']['Name'] ?? 'no name given';
    
    $payout_array[] = [
        'name' => $name,
        'total_payout' => $total = $value['fields']['Amount'] ?? '0'
    ];

    print $value['Name'] . ' $' . number_format($total, 2) . '<br/>';
}
  • that got me pretty close however I keep getting an error that says Undefined Array Key 'Name' on line 59 which is the print statement before the last closing brace. many of these do seem to come from the code provided in my question so is there any chance you could explain where you had gotten that from? I'm just trying to trace it back to make sure I have all upper and lower cases correct. Also it does show me the amount paid to each employee but is there a way to total it so there is only one number showing per name? The end result should have two names and two numbers. Thanks! – masha May 08 '21 at 00:44
  • Where there is a will, there's a way... This feels like a homework question so I'm going to let you fix the rest. I would start by adding `$name` as a key value to the array you are creating; `$payout_array[$name]`. The issue with `$value['Name']` is also very trivial, as it has multiple other correct references it can be replaced with ;) Finally, setting the total payout might look like `$value['fields']['Amount'] + $payout_array[$name]['total_payout']`. Welcome to Stack Overflow, btw! – Richard Tyler Miles May 08 '21 at 00:54
  • You'll need to check [proper error checking](https://stackoverflow.com/questions/34571330/php-ternary-operator-vs-null-coalescing-operator) too since the $payout_array may not have values in it first run. `$value['fields']['Amount'] ?? '0'` above is a good example of that. – Richard Tyler Miles May 08 '21 at 00:56
  • 1
    This is not a homework assignment. I'm doing this to learn and teach myself php. otherwise I would ask a teacher. I do appreciate the help, but I still can't get it to do the math right. There is something wrong with my syntax and I really just need someone to help me so I can learn. – masha May 08 '21 at 01:24
  • when I try to calculate the totals it only adds like the first two for each person. I tried using array_sum() but it gives me an error "Uncaught TypeError: array_sum(): Argument #1 ($array) must be of type array, float given in C:\xampp\htdocs\DevTest\payout.php:49 Stack trace: #0 C:\xampp\htdocs\DevTest\payout.php(49): array_sum(777.21) #1 {main} thrown in C:\xampp\htdocs\DevTest\payout.php on line 49". Could you help me with this or maybe tell me who can or where I can find documentation online to help me figure it out? – masha May 08 '21 at 01:33
  • Can you post your updated code and the full sample as text? – Richard Tyler Miles May 12 '21 at 01:01