I am having trouble accessing information via PHP coming from json in javascript.
I use localstorage to save some temporary data:
var tbRomaneio = localStorage.getItem("tbRomaneio");// Recupera os dados armazenados
tbRomaneio = JSON.parse(tbRomaneio); // Converte string para objeto
if(tbRomaneio == null) { // Caso não haja conteúdo, iniciamos um vetor vazio
tbRomaneio = new Array();
}
//Item
var medida = JSON.stringify({
comprimento : medidaComprimento,
largura : medidaLargura,
token : token.toString()
});
tbRomaneio.push(medida);
localStorage.setItem("tbRomaneio", JSON.stringify(tbRomaneio));
My code in javascript to post:
$.post('/pedido/salva-romaneio', {itens: localStorage.getItem("tbRomaneio")} )
.done(function(data) {
//ok
})
So far so good. The problem is in PHP, when I read this information it returns error.
Here's my PHP code:
<?php
$itensRomaneio = json_decode($_POST['itens'], true);
print_r($itensRomaneio);
?>
Array
(
[0] => {"comprimento":"230","largura":"54","token":"1495719950761"}
)
When I read the array I can not access the information, it gives the following error:
//Array
for($i = 0; $i < count($itensRomaneio); $i++) {
echo $itensRomaneio[$i]->token;
}
Error:
<p>Severity: Notice</p>
<p>Message: Trying to get property of non-object
And if I try to use it this way it only returns me this:
//Array
for($i = 0; $i < count($itensRomaneio); $i++) {
echo $itensRomaneio[$i]['token'];
}
Return only this:
" { "
If I give a print_r it is shown:
print_r($itensRomaneio[$i]);
//show
{"comprimento":"230","largura":"54","token":"1495719950761"}
What is going on?