Foreach PHP JSON json_decode How to print sub array

Asked

Viewed 1,048 times

5

Good morning folks! My question is this. 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 don’t know how to print the "attendees" sub array. Does anyone know how to do so I can also print the sub array

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

See the full 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>
  • Participants are missing the [ ' square parentheses to place them as vector. Next, you do the same process of the events and you should print... What do you say?

  • In the api I’m pulling it looks like the model below { "total": 1, "events": [ { "id": "1", "status": "in progress", "title": "Demo Event", "Description": "Just a test event", "logotag_hotsite": "http://", "logotag_event": "http://", "start_date": "10/02/2018", "data_termino": "11/02/2018", "participants": { "totals": 10, "confirmed": "5", "nao_confirmed": "3", "disabled": "2" }, "link_hotsite": "http://", "link_event": "http://", "link_registration": "http://", } ] }

  • @Leodario edit your question and put your code there, otherwise it becomes difficult to understand.

  • I cloned the full code

2 answers

0


Hello,

I prefer to work with Arrays! Putting true in the second parameter of json_decode( $json, true). json_decode - PHP Manual

$eventos = $obj_json_trans[ 'eventos' ];

// LOOP EVENTOS
foreach ( $eventos as $k => $v ) {

    // MOSTRA OS VALORES COM QUEBRA DE LINHA
    echo "$k - $v <br>";

    // SE CHAVE É PARTICIPANTES E É ARRAY, LOOP E MOSTRA OS PARTICPANTES
    if($k === 'participantes' && is_array($v)){

        // LOOP PARTICIPANTES
        foreach($v as $k2 => $v2){

            echo "- $k2 = $v2 <br>";

        } // LOOP PARTICPANTES

    } // IF PARTICPANTES

} // LOOP EVENTOS

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>";                 
  }
}

Browser other questions tagged

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