Browse variable type php array

Asked

Viewed 883 times

0

I have a $array variable that contains array values that were received from a JSON.stringify(venc)

[
{"Nº da Parc":"1","Data do pagamento":"06/04/2017","valor a pagar":"R$ 50,00"},
{"Nº da Parc":"2","Data do pagamento":"06/05/2017","valor a pagar":"R$ 50,00"},
{"Nº da Parc":"3","Data do pagamento":"06/06/2017","valor a pagar":"R$ 50,00"},
{"Nº da Parc":"4","Data do pagamento":"06/07/2017","valor a pagar":"R$ 50,00"},
{"Nº da Parc":"5","Data do pagamento":"06/08/2017","valor a pagar":"R$ 50,00"}
]

I was able to display only with the echo $array; .

How do I go through this variable?

I tried to use the

foreach($array as $d){
  echo $d;
}

and displays nothing

and trying to use $arr = json_decode($array); the php file gives error, No message, just red on Chrome’s bugger

inserir a descrição da imagem aqui

  • In PHP, associative arrays are also defined between [], nay {}, outside that is not used : in the assignment and yes =>. See working on Repl.it

  • is because these values came from JSON.stringify(venc); ai in php I’m looking forward to going through them

  • But if it’s coming this way in PHP, you need to convert JSON to array instead. For this, there is the function json_decode.

  • every time I do $var = json_decode($array) gives error in php file. Using var_dump(json_decode($array) works, but I’d like to scroll through to show in an organized way. How to convert json to array?

  • With this function. Edit your question and post all your code. It’s hard to understand what you’re doing. Put together the error that gives.

  • but the file only turns red on that Chrome bugger and doesn’t say what error is

Show 1 more comment

2 answers

2


I tried it this way and it seems to work

$json= '[
{"Nº da Parc":"1","Data do pagamento":"06/04/2017","valor a pagar":"R$ 50,00"},
{"Nº da Parc":"2","Data do pagamento":"06/05/2017","valor a pagar":"R$ 50,00"},
{"Nº da Parc":"3","Data do pagamento":"06/06/2017","valor a pagar":"R$ 50,00"},
{"Nº da Parc":"4","Data do pagamento":"06/07/2017","valor a pagar":"R$ 50,00"},
{"Nº da Parc":"5","Data do pagamento":"06/08/2017","valor a pagar":"R$ 50,00"}
]';



$data = json_decode($json);
foreach ($data as $name => $value) {
        echo '  ' . $value->{'Nº da Parc'} . "<br>";
        echo '  ' . $value->{'Data do pagamento'} . "<br>";
        echo '  ' . $value->{'valor a pagar'} . "<br>";

}
  • is because these values came from JSON.stringify(venc); this way ai in php I want to go through them

  • What do you mean you can’t use it the way I put it now?

  • is because Json already sends that way

  • Ready I edited try so

  • Bingoooo!! Exactly that! Thank you so much!

0

This is due to the inability of php to work directly with Json. You need to convert it to an associative array via the method json_decode.

Just pass the string containing the json to the method and it will return an array:

$resultado = json_decode($json);

giving a var_dump you will see that the result is an associative array, which can be traversed through a foreach

  • Not to be annoying, but in what your reply adds to the discussion that was no longer said in the above answer?

  • I explain why it doesn’t work.

  • but it’s important information

Browser other questions tagged

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