Go through php array variable

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 echo $array; .

How do I go through this variable?

I tried to use

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

and nothing displays

and when trying to use $arr = json_decode($array); the php file gives error, No message, just red in chrome debugger

    
asked by anonymous 07.03.2017 / 03:27

2 answers

2

I tried 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>";

}
    
07.03.2017 / 03:35
0

This is due to the non-ability of php to work directly with Json. You need to convert it to an associative array using the json_decode method.

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

$resultado = json_decode($json);

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

    
07.03.2017 / 14:25