M
M
Mark Eshkilev2021-06-15 00:02:58
PHP
Mark Eshkilev, 2021-06-15 00:02:58

How should the json look like for the given php code?

Hello, I spent 40 minutes on how json should look like for this code, but I still don’t understand, please help

<?php
              $json = <какой то json>;
              $mapstats = json_decode($json, true);
              $totalUsed = 0;
              foreach ($mapstats as $i)
                $totalUsed += $i['usageCount'];
          
              foreach ($mapstats as $key => $value)
                echo('{ name: \'' . $mapstats[$key]['map'] . '\', y: ' . str_replace(',','.',($mapstats[$key]['usageCount'] / $totalUsed) * 100) . ' }' . (($key != (count($mapstats) - 1)) ? ",\n" : ''));
            ?>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
MaLuTkA_UA, 2021-06-15
@MaLuTkA_UA

Something like this...

<?php
$json = <какой то json>;
$mapstats = json_decode($json, true);
$totalUsed = 0;
foreach ($mapstats as $i) {
    $totalUsed += $i['usageCount'];
}          
$data = [];
foreach ($mapstats as $value) {
    $data[] = [
        "name" => $value["map"],
        "y" => $value["usageCount"] / $totalCount * 100
    ];
}
echo json_encode($data);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question