Reputation: 504
This is my Controller:
public function index(Request $request)
{
$soapClient = new \SoapClient('http://187.32.223.233/UraIntegrada/Siacon/ConsorcioServices.asmx?WSDL');
$params = array(
'sGrupo' => "0".$request->sGrupo,
'iCota' => $request->iCota,
'iIdERP' => $request->iIdERP
);
$response = $soapClient->ROS_ExtratoContaCorrente($params);
foreach ($response as $dados) {
$dado = json_decode(json_encode($dados), true);
}
// return $dado;
// die();
return view('layouts.extrato', compact('dado'));
}
And this is how I am rendring data on blade:
<div class="col">
Nome: {{ $dado['NOME-CLIENTE'] }}<br>
Rua: {{ $dado['NOME-LOGRADOURO'] }}, Nº{{ $dado['NUMERO-LOGRADOURO'] }}<br>
Bairro: {{ $dado['BAIRRO'] }}<br>
Cidade: {{ $dado['CIDADE'] }} - UF: {{ $dado['UF'] }}<br>
CEP: {{ $dado['CEP'] }}<br>
Telefone: {{ $dado['NUMERO-TELEFONE'] }}
</div>
How do I access a JSON nested object with Laravel?
Can access via $dados['NOME-CLIENTE'], but don´t know how to access the next nested object 'PARCELAS-PAGAS`->PARCELA-PAGA->NUMERO-PARCELA
{
"NOME-CLIENTE": "JANE DOE",
"CODIGO-GRUPO": "06275",
"NUMERO-COTA": 45,
"NOME-LOGRADOURO": "JUNIOR STREET",
"NUMERO-LOGRADOURO": "519",
"BAIRRO": "PRESIDENTE ROOSEVELT",
"CIDADE": "UBERLANDIA",
"PARCELAS-PAGAS": {
"PARCELA-PAGA": [
{
"NUMERO-PARCELA": 17,
"DATA-VENCIMENTO": "20190108",
},
Now I want to access the 'NUMERO-PARCELA' node, but don´t know how to do it in Laravel
Upvotes: 0
Views: 207
Reputation: 62016
If the overall object is an associative array, which it appears to be from your example code, then you'd access your property like this:
$dado["PARCELAS-PAGAS"]["PARCELA-PAGA"][0]["NUMERO-PARCELA"]
The [0]
index will get you the first item in the "PARCELA-PAGA" array (this bit is a numeric array rather than an associative array).
Here's a runnable demo: http://sandbox.onlinephpfunctions.com/code/54097b7213fbfe598c67a2c08b43d1e72ba2ede6
Of course if you expect there might be multiple items, you'd be better to use a loop to fetch each on in turn, assuming you want to get them all.
P.S. Forget about it being JSON, once it's been de-serialized into a variable then it works like any other PHP variable. You can always use var_dump()
to look at the structure of the variable once it's become a PHP variable.
Upvotes: 1
Reputation: 8750
The key PARCELA-PAGA
is an array. You need to loop over it.
For example:
$json = '{
"NOME-CLIENTE": "JANE DOE",
"CODIGO-GRUPO": "06275",
"NUMERO-COTA": 45,
"NOME-LOGRADOURO": "JUNIOR STREET",
"NUMERO-LOGRADOURO": "519",
"BAIRRO": "PRESIDENTE ROOSEVELT",
"CIDADE": "UBERLANDIA",
"PARCELAS-PAGAS": {
"PARCELA-PAGA": [
{
"NUMERO-PARCELA": 17,
"DATA-VENCIMENTO": "20190108"
}
]
}
}';
$dado = json_decode($json, true);
$parcelaPaga = $dado['PARCELAS-PAGAS']['PARCELA-PAGA'];
foreach($parcelaPaga as $parcela) {
echo $parcela['NUMERO-PARCELA'] . PHP_EOL;
}
Here's an example you can play with.
Upvotes: 1