How to get the value of this Array in php?

Asked

Viewed 67 times

-1

I’m doing an Axios.post and sending an object with JSON.stringfy()

In my php file I gave var_dump in the reply and $POST returns an array like this:

array(1) ' ["{"plate":"","ano_model":"","ano_fabr":"","chassis":","Renavam":"","brand name":"",

with the print_r it comes like this:

[{"plate":"","ano_model":"","ano_fabr":"","chassis":"","Renavam":"","tag":""

But I can’t get the value of this array in my function, I’ve tried $body['board'] and nothing.. if anyone can help me.

  • Apparently you are not using json_decode (https://www.php.net/manualen/function.json-decode.php) when receiving data via post in PHP.

  • That’s not it, when I try to use Decode it says it’s not a string, just because I have an array, as shown above

  • So, it’s to use in array elements.

  • How do you really get to your string? With print_r?

1 answer

0

You can use the json_decode to read the string in json format and then convert it to json object

For example:

<?php

   $value = '[{"placa":"JXK02939","ano_modelo":"","ano_fabr":"","chassi":"","renavam":"","marca":""}]';
   $jsonValue = json_decode( $value );
   $values = $jsonValue[0];
   echo "<blockquote>";
   echo "Placa: ".$values->placa."<br>";
   echo "Ano Modelo: ".$values->ano_modelo."<br>";
   echo "Ano Fabricacao: ".$values->ano_fabr."<br>";
   echo "Chassi: ".$values->chassi."<br>";
   echo "Renavam: ".$values->renavam."<br>";
   echo "Marca: ".$values->marca."<br>";
   echo "</blockquote>";
?>

If it’s just an object

If you want to loop you can use the foreach

foreach ($jsonValue as $key => $value) {
       echo "<blockquote>";
       echo "Placa: ".$value->placa."<br>";
       echo "Ano Modelo: ".$value->ano_modelo."<br>";
       echo "Ano Fabricacao: ".$value->ano_fabr."<br>";
       echo "Chassi: ".$value->chassi."<br>";
       echo "Renavam: ".$value->renavam."<br>";
       echo "Marca: ".$value->marca."<br>";
       echo "</blockquote>";
   }

I hope I’ve helped..

  • So what happens is that as I showed above, I don’t have a string outside the array to use Decode

  • You have a string array with json format?

Browser other questions tagged

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