Organize a json and write the results

Asked

Viewed 306 times

0

I have the following 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]
  }
]
}

In each json field I have one array, how do I write the products in order?

example:

10 mouse 10 UN
3 teclado 2 un
4 monitor 5 un
11 webcam 1 un
1 celular 0 cx
  • Associate key to value?

  • I edited my question, I want to put in a list the products. However the codes, description, stock and unit. are in separate array.

  • But the json is badly formatted, it would act if you put it well, to be able to test more easily

  • 1

    For the record, this JSON is invalid.

2 answers

2


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": ...
  },
]
  • 1

    OK vlw, I’ve fixed json tbm. Thank you very much

0

You turn json into an object and can use for to read each object. Something more or less like this (assuming everything, COD, DESCRIPTION, STOCK and QUANT_UNID have the same number of elements):

$valores = "";
$obj = json_decode($dadosJson, TRUE);
for($i=0; $i<count($obj['COD']); $i++) {
    $valores .= $obj['COD'][$i] . " " .$obj['DESCRICAO'][$i] . " " . $obj['ESTOQUE '][$i] . " " . $obj['QUANT_UNID'][$i] . "\n";
}
echo $valores;
  • Friend sorry, but I need to inform you, this wrong, missing the results.

  • Thanks @Guilhermenascimento, really missed, so I wrote that it would be something like this, the idea was shows how to make the loop.

  • 1

    One tool to test online you can use is http://ideone.com

Browser other questions tagged

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