How to add data from an array or json in PHP

Asked

Viewed 198 times

0

Hello, I have a PHP array with data coming from the database, so it’s not static data, it changes. I need to add the wrong passes, goal kicks, etc of each player and save in a variable, because I want to put in a graph. I have tried to do by array and json_decode, but only find examples of how to get key data, however my array has several values in each key.

{"dados":[
    {"atleta":"Lavoisier","passeErrado":3,"chuteAgol":1,"perdida":1,"interceptacao":0},
    {"atleta":"Lavoisier","passeErrado":3,"chuteAgol":1,"perdida":1,"interceptacao":0},
    {"atleta":"Lavoisier","passeErrado":3,"chuteAgol":1,"perdida":1,"interceptacao":0},
    {"atleta":"Lavoisier","passeErrado":3,"chuteAgol":1,"perdida":1,"interceptacao":0},
    {"atleta":"Lavoisier","passeErrado":3,"chuteAgol":1,"perdida":1,"interceptacao":0},
    {"atleta":"Bage ","passeErrado":1,"chuteAgol":0,"perdida":0,"interceptacao":1},
    {"atleta":"Bage ","passeErrado":0,"chuteAgol":0,"perdida":1,"interceptacao":0},
    {"atleta":"Lavoisier ","passeErrado":1,"chuteAgol":2,"perdida":1,"interceptacao":1},
    {"atleta":"Bage ","passeErrado":0,"chuteAgol":0,"perdida":1,"interceptacao":0},
    {"atleta":"Lavoisier ","passeErrado":1,"chuteAgol":2,"perdida":1,"interceptacao":1},
    {"atleta":"Lavoisier ","passeErrado":0,"chuteAgol":1,"perdida":0,"interceptacao":1}
]}

3 answers

1

You have to use the json_decode to be able to use the data in PHP, in the example I set the data and values of the athletes, using the athlete’s name as the index of the array.


$json = json_decode('{"dados":[
    {"atleta":"Lavoisier","passeErrado":3,"chuteAgol":1,"perdida":1,"interceptacao":0},
    {"atleta":"Lavoisier","passeErrado":3,"chuteAgol":1,"perdida":1,"interceptacao":0},
    {"atleta":"Lavoisier","passeErrado":3,"chuteAgol":1,"perdida":1,"interceptacao":0},
    {"atleta":"Lavoisier","passeErrado":3,"chuteAgol":1,"perdida":1,"interceptacao":0},
    {"atleta":"Lavoisier","passeErrado":3,"chuteAgol":1,"perdida":1,"interceptacao":0},
    {"atleta":"Bage ","passeErrado":1,"chuteAgol":0,"perdida":0,"interceptacao":1},
    {"atleta":"Bage ","passeErrado":0,"chuteAgol":0,"perdida":1,"interceptacao":0},
    {"atleta":"Lavoisier","passeErrado":1,"chuteAgol":2,"perdida":1,"interceptacao":1},
    {"atleta":"Bage ","passeErrado":0,"chuteAgol":0,"perdida":1,"interceptacao":0},
    {"atleta":"Lavoisier","passeErrado":1,"chuteAgol":2,"perdida":1,"interceptacao":1},
    {"atleta":"Lavoisier","passeErrado":0,"chuteAgol":1,"perdida":0,"interceptacao":1}
]}');

$atleta = [];

foreach ($json->dados as $dado) {
  foreach ($dado as $key => $value) {
    if ($key == 'atleta') {
      continue;
    }
    if (isset($atleta[$dado->atleta][$key])) {
      $atleta[$dado->atleta][$key] += $value;
    } else {
      $atleta[$dado->atleta][$key] = $value;
    }
  }
}

var_dump($atleta);

Exit


array(2) {
  ["Lavoisier"]=>
  array(4) {
    ["passeErrado"]=>
    int(17)
    ["chuteAgol"]=>
    int(10)
    ["perdida"]=>
    int(7)
    ["interceptacao"]=>
    int(3)
  }
  ["Bage "]=>
  array(4) {
    ["passeErrado"]=>
    int(1)
    ["chuteAgol"]=>
    int(0)
    ["perdida"]=>
    int(2)
    ["interceptacao"]=>
    int(1)
  }
}

EDIT

Using the same logic only by removing the athlete we have the total of attributes


$acoes = [];

foreach ($json->dados as $dado) {
  foreach ($dado as $key => $value) {
    if ($key == 'atleta') {
      continue;
    }
    if (isset($acoes[$key])) {
      $acoes[$key] += $value;
    } else {
      $acoes[$key] = $value;
    }
  }
}
var_dump($acoes);

Exit

array(4) {
  ["passeErrado"]=>
  int(18)
  ["chuteAgol"]=>
  int(10)
  ["perdida"]=>
  int(9)
  ["interceptacao"]=>
  int(4)
}
  • This I have tried. I really want to add those numbers, extracting them for a variable. If the player is so-and-so, sum up all his wrong passes and save them in a variable. What you suggest you can do with a print_r() or a var_dump().

  • @Paulolara The answer no longer does that?

  • No. If I want to play the total action sum variable for each player, into a graph, I’m not getting this variable. The question is, how do I enter the sum of each player’s action into variables?

-2

I got with the code below just get the sum TOTAL of each action. I need to get the sum per player. Can help?

$stmt1 = mysqli_query($conn, $sql1);
$totalPasses = 0;
$totalChuteg = 0;
$totalPerdid = 0;
$totalInterc = 0;
while ($row = mysqli_fetch_assoc($stmt1) ) {
    $atleta = $row['atleta'];
    $passeErrado = (int)$row['passeErrado'];
    $chuteAgol = (int)$row['chuteAgol'];
    $perdida = (int)$row['perdida'];        
    $interceptacao = (int)$row['interceptacao'];
    $array["dados"] = array('atleta' => $atleta, 'passeErrado' => $passeErrado, 'chuteAgol' => $chuteAgol, 'perdida' => $perdida, 'interceptacao' => $interceptacao);

    foreach ($array as $dados) {                
        foreach ($dados as $key => $value) {
            if($key == 'passeErrado'){
                $totalPasses = $totalPasses + $value;
            }
            if($key == 'chuteAgol'){
                $totalChuteg = $totalChuteg + $value;
            }
            if($key == 'perdida'){
                $totalPerdid = $totalPerdid + $value;
            }
            if($key == 'interceptacao'){
                $totalInterc = $totalInterc + $value;
            }
         }
    }
}
echo "totalPasses ".$totalPasses."</br>";
echo "totalChuteg ".$totalChuteg."</br>";
echo "totalPerdid ".$totalPerdid."</br>";
echo "totalInterc ".$totalInterc."</br>";

Upshot:

totalPasses 96
totalChuteg 99
totalPerdid 98
totalInterc 70

But I would like to know the actions individually per player.

-2


I made this json array go to Android and there I dealt with Java, and making it return with Volley for every player filter I wished. Thankful.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.