-1

i parse json document and put date in table and put id type (example 2, 3, 4) .

how to calc sum by id?

my code

<?php
$json=file_get_contents("https://pv.cec.md/app1/api/reports/GetElectionPresence?electionType=2");
$data =  json_decode($json);

 if (count($data->Circumscriptions)) {
        // Open the table
        echo "<table>";

        // Cycle through the array
        foreach ($data->Circumscriptions as $idx => $Circumscriptions) {
$a = $Circumscriptions->TotalVoted;
            // Output a row
            echo "<tr>";
            echo "<td id='$Circumscriptions->Type'>$Circumscriptions->TotalVoted</td>";
            echo "</tr>";
        }

        // Close the table
        echo "</table>";
$b=array($a);
echo "Total = ". array_sum($b);;
    }
?>

and i put it to online fidget https://paiza.io/projects/N9CHMtFrLp9fWqoE1uhvTQ

  • Seeing "view" mixed with "logic" but okay. To sum a date, convert both to a numerical value, add, then convert back. – GetSet Nov 04 '20 at 11:32
  • ok but how to convert? – user3555881 Nov 04 '20 at 11:33
  • If you could show an excerpt of the JSON – GetSet Nov 04 '20 at 11:40
  • When you ask a question, we don't need to see your whole script. We only need to see enough code and data to reproduce the problem. We don't need the first 8 lines of your snippet and we don't need to see any of the html generation. We want sample input, minimal code, and your exact desired output. Please read all of [ask]. – mickmackusa Nov 04 '20 at 13:18
  • @mickmackusa How do you determine that this specific question is a duplicate of "How to add elements to an empty array in PHP?" ? For me, it seems quite unrelated. – jonasdev Nov 04 '20 at 13:39
  • 1
    Whoops, my earlier comment had incorrect code... it should have read "`$b[] = $Circumscriptions->TotalVoted;` needs to be written inside the loop." @jonas the OP was already iterating the objects and trying to assign each iteration's value to a temporary variable (intended to be an array), so that after the loop, the `array_sum()` would work properly. Because they were overwriting `$a` each time instead of pushing the values into the temporary array, `array_sum()` could not work. The solution is to understand how to push elements into an array. Job done. – mickmackusa Nov 04 '20 at 14:06
  • Alternatively, I could have found a duplicate that demonstrates `array_sum(array_column())` ... all basic techniques are duplicates on Stack Overflow. – mickmackusa Nov 04 '20 at 14:08

1 Answers1

0

You can add functionality to your loop to create a $totals array which contains the total number of votes for each id, with something like this:

    $totals = [];
    foreach ($data->Circumscriptions as $idx => $Circumscriptions) {
        $a = $Circumscriptions->TotalVoted;
        // Output a row
        echo "<tr>";
        echo "<td id='$Circumscriptions->Type'>$Circumscriptions->TotalVoted</td>";
        echo "</tr>";

        if (empty($totals[$Circumscriptions->Type])) {
             $totals[$Circumscriptions->Type] = 0;
        }
        $totals[$Circumscriptions->Type] += $Circumscriptions->TotalVoted;
    }

The $totals variable will then contain data sorted by id, which you can loop over to print or use elsewhere, for example like this:

    foreach ($totals as $id => $total) {
        echo "Total of id $id = $total \n";
    }
jonasdev
  • 696
  • 5
  • 11
  • Please help us to close basic duplicate questions. After more than a decade of feverish Q&A, all basic questions have been asked many times. We don't need to add to the redundancy. Please read that not every question needs to be answered @ [answer]. – mickmackusa Nov 04 '20 at 13:15