Foreach PHP JSON json_decode How to print sub array

4

Good morning everyone! My question is as follows. I created a foreach to access the data below an API

$token = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";

$headers = array('Authorization: Token ' . $token);

$ch_subs = curl_init();
curl_setopt($ch_subs, CURLOPT_URL, 'https://dominio.site.com/api/listar');
curl_setopt($ch_subs, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch_subs, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch_subs, CURLOPT_HTTPHEADER, $headers);
$obj_json_trans = json_decode(curl_exec($ch_subs));{
"total": 1,
"eventos": [
    {
        "id": "1",
        "status": "em andamento",
        "titulo": "Evento Demo",
        "descricao": "Apenas um evento de teste",
        "logomarca_hotsite": "http://",
        "logomarca_evento": "http://",
        "data_inicio": "10/02/2018",
        "data_termino": "11/02/2018",
        "participantes": {
            "totais": 10,
            "confirmados": "5",
            "nao_confirmados": "3",
            "desativados": "2"
        },
        "link_hotsite": "http://",
        "link_evento": "http://",
        "link_inscricao": "http://",
    }   ] }      $eventos = $obj_json_trans->eventos;      
        foreach ( $eventos as $e )
        {
            echo "id: $e->id - status: $e->status - título: $e->titulo<br>";                 
        }
        curl_close($ch_subs);

I can print the arrays but I do not know how to print the sub-array "participants". Does anyone know how to do this so I can also print the sub array

"participantes": {
        "totais": 10,
        "confirmados": "5",
        "nao_confirmados": "3",
        "desativados": "2"
    },

See the complete code

<?php

$token = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";

$headers = array('Authorization: Token ' . $token);

$ch_subs = curl_init();


curl_setopt($ch_subs, CURLOPT_URL, 'https://site.com/api/listar');
curl_setopt($ch_subs, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch_subs, CURLOPT_SSL_VERIFYPEER, false);

curl_setopt($ch_subs, CURLOPT_HTTPHEADER, $headers);

$obj_json_trans = json_decode(curl_exec($ch_subs));


/* print_r($obj_json_trans); */

?>
<!DOCTYPE html>
<html lang="pt-br">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <?php
        $eventos = $obj_json_trans->eventos;    
        foreach ( $eventos as $chave=>$e )
        {
            echo "id: $e->id - status: $e->status - título: $e->titulo <br> ";                  

        }

        curl_close($ch_subs);
    ?>
</body>
</html>
    
asked by anonymous 07.09.2018 / 12:14

1 answer

0
foreach ($eventos as $e) {
  echo "id: $e->id - status: $e->status - título: $e->titulo<br>";
  foreach ($e->participantes as $key => $value) {
    echo "$key: $value <br>";                 
  }
}
    
14.09.2018 / 21:50