Just for the record, this JSON is in invalid format:
{
"result":[
{
“COD”:[10,3,4,11,1],
"DESCRICAO”:[mouse,teclado,monitor,webcam,celular],
"ESTOQUE”:[10,2,5,1,0],
"QUANT_UNID":[ UN,UN,UN,UN,CX]
}
]
}
Correct him first, it should look like this:
{
"result":[
{
"COD":[10,3,4,11,1],
"DESCRICAO":["mouse", "teclado", "monitor", "webcam", "celular" ],
"ESTOQUE":[10,2,5,1,0],
"QUANT_UNID":[ "UN", "UN", "UN", "UN", "CX" ]
}
]
}
Once fixed (I suppose it is dynamic) just use one for
if all items have the same amount:
<?php
$json = ' {
"result":[
{
"COD":[10,3,4,11,1],
"DESCRICAO":["mouse", "teclado", "monitor", "webcam", "celular" ],
"ESTOQUE":[10,2,5,1,0],
"QUANT_UNID":[ "UN", "UN", "UN", "UN", "CX" ]
}
]
}';
$parsed = json_decode($json);
$results = $parsed->result;
foreach ($results as $item) {
$cod = $item->COD;
$qtd = $item->QUANT_UNID;
$estoque = $item->ESTOQUE;
$descricao = $item->DESCRICAO;
$j = count($cod);
for ($i = 0; $i < $j; $i++) {
echo $cod[$i], ' ';
echo $descricao[$i], ' ';
echo $qtd[$i], ' ';
echo $estoque[$i], '<br>';
}
}
If it is an HTML table:
for ($i = 0; $i < $j; $i++) {
echo '<tr>';
echo '<td>', $cod[$i], '</td>';
echo '<td>', $descricao[$i], '</td>';
echo '<td>', $qtd[$i], '</td>';
echo '<td>', $estoque[$i], '</td>';
echo '</tr>';
}
In case I used the foreach
because I suppose Results can receive multiple data, like:
"result":[
{
"COD": ...,
"DESCRICAO": ...,
"ESTOQUE": ...,
"QUANT_UNID": ...
},
{
"COD": ...,
"DESCRICAO": ...,
"ESTOQUE": ...,
"QUANT_UNID": ...
},
{
"COD": ...,
"DESCRICAO": ...,
"ESTOQUE": ...,
"QUANT_UNID": ...
},
]
Associate key to value?
– Miguel
I edited my question, I want to put in a list the products. However the codes, description, stock and unit. are in separate array.
– Hugo Borges
But the json is badly formatted, it would act if you put it well, to be able to test more easily
– Miguel
For the record, this JSON is invalid.
– Guilherme Nascimento