Access Json Multidimensional PHP/Laravel

Asked

Viewed 256 times

1

I am trying to access a multidimensional Json via Windows and I am not getting the expected result, I need to access phases->games->data->2017-05-13 I will post the code below to find a solution:

My array:

inserir a descrição da imagem aqui

Controller.php

public function index(){



    // JSON DOS ARQUIVOS
    $arquivo_json = file_get_contents('http://jsuol.com.br/c/monaco/utils/gestor/commons.js?file=commons.uol.com.br/sistemas/esporte/modalidades/futebol/campeonatos/dados/2017/30/dados.json');
    $dadosJson = json_decode($arquivo_json, true);

    return view('ler', compact('dadosJson'));
}

view php.

@foreach($dadosJson['fases'] as $equipes)
        @foreach($equipes['jogos'] as $jogos)
            {{ $jogos['nome'] }}
        @endforeach

    @endforeach

return inserir a descrição da imagem aqui

Could someone give me a hand?

Thank you!

  • You seem to be right. Maybe you have some data of jogos that does not exist. Make a condition before checking if the key exists to continue. if(array_key_exists('data', $jogos')).

  • Good morning @Diego-Souza, I did as follows foreach($dadosJson['phases'] the $teams) if(array_key_exists('data', $games')) foreach($teams['games'] the $games) {{ $games['name'] }} endforeach endforeach !

1 answer

1

I was able to access by print_r, but to visualize can not always returns me the error:

changes to the controller.php

return view('ler')->with('dadosJson', $dadosJson);

return by print_r

Array(
[0] => 97071
[1] => 97073)
     1            
                Array(
[0] => 97071
[1] => 97073)
     1           
                Array(
[0] => 97071
[1] => 97073)

return without the print_r {{ $teams['games']['data']['2017-05-13'] }} inserir a descrição da imagem aqui

Some help?

Browser other questions tagged

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