Hello, friends, how are you, I have problems like going through this json, I don't know if it is multiple or dimensional, there we go, I am consuming a web service and it returns me that json where it shows me 2 main arrays and within each array there are more arrays, 1 or more for go through the main arrays I do a for and for the secondary ones I do another for but the issue is that when it has only one array inside the main one it does not show but in case it has more than 1 one yes, I do not know if I will be doing it right.
In the image shows the json, the correct thing should be that the seat 19473575 only shows 1 time and the other seat 15120251 is showing the 3 that I have inside.
Any suggestion would be very helpful.
{"listarAsientosResponse":{"asientos":{"transaccion":13978138,"nroTotalPag":4,"listAsientos":[{"idImgAsiento":19473575,"numPag":1,"tipo":"ASIENTO","listPag":{"nroPagRef":4,"pagina":1}},{"idImgAsiento":15120251,"numPag":3,"tipo":"ASIENTO","listPag":[{"nroPagRef":1,"pagina":1},{"nroPagRef":2,"pagina":2},{"nroPagRef":3,"pagina":3}]}]}}}
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://ws3.pide.gob.pe/Rest/Sunarp/ListarAsientos?......&out=json",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
));
$response = curl_exec($curl);
$ressunartdetalle = json_decode($response, true);
$dato1 = $ressunartdetalle['listarAsientosResponse']['asientos']['listAsientos'];
$logitud1 = count($dato1);
?>
<table>
<?php
for ($i = 0; $i < $logitud1; $i++) {
$dato2 = $ressunartdetalle['listarAsientosResponse']['asientos']['listAsientos'][$i]['listPag'];
$largo2 = count($dato2);
for ($k = 0; $k < $largo2; $k++) {
?>
<tr>
<td class='text-center'><?php echo $dato1[$i]['idImgAsiento']; ?></td>
<td class='text-center'><?php echo $dato1[$i]['numPag']; ?></td>
<td class='text-center'><?php echo $dato1[$i]['tipo']; ?></td>
<td class='text-center'><?php echo $dato2[$k]['nroPagRef']; ?></td>
<td class='text-center'><?php echo $dato2[$k]['pagina']; ?></td>
</tr>
<?php
}
}
?>
</table>
Well, answering myself, I just solved it, I forgot to put it here too, in case someone has this little problem in the future.
In the first for is to traverse the main arrays. In the second for is to go through the secondary arrays within the main ones and also if it only has 1 or more secondary arrays.
Ok guys, that's how I fixed it.