0

I have this json array output:

{"Results":{"Info":[{"type":"INFO","code":"XSD","category":"XSD Val","message":"Very good result","status":"PASS"}],"warningMessages":[],"errorMessages":[],"status":"PASS"},"Status":"OK","clearanceStatus":null,"qrSellertStatus":null,"qrBuyertStatus":null}

And want to pass varray values inside php variable:

$type="INFO";
$message = "Very good result";
$status = "PASS";

How can I achieve it in php ?

I tried to use json_decoded

 $json = json_decode($array);

echo $json ['Results']['Info']['type'];
echo $json ['Results']['Info']['type']['message'];

But I got this error:

Fatal error: Uncaught Error: Cannot use object of type stdClass as array in /home/

Thanks for all

MK Said
  • 165
  • 8
  • 1
    Did you try anything? Please add your code-effort to your question – Alive to die - Anant Oct 11 '22 at 12:20
  • @Anant-Alivetodie yes you are right, I added my trial :) – MK Said Oct 11 '22 at 12:24
  • 1
    This code does indeed error, but doesn't produce the error you said it does: https://3v4l.org/bnVck – ADyson Oct 11 '22 at 12:26
  • 2
    https://3v4l.org/UJ7GT is probably what you intended. It a) decodes the JSON as an associative array, so that your `[]` access syntax will work, and b) takes into account that the "Info" property contains an array, and so accesses the first index of that array before trying to access any named properties within it, and c) takes into account that "message" isn't a sub-property of "type", it's on the same level. – ADyson Oct 11 '22 at 12:28
  • Your question is a duplicate of: [How to extract and access data from JSON with PHP?](https://stackoverflow.com/questions/29308898/how-to-extract-and-access-data-from-json-with-php). Read the accepted answer there, learn the techniques and how to interpret the data structure, and then you should be able to access any such data, regardless of the specifics. – ADyson Oct 11 '22 at 12:28
  • @ADyson yes sorry you are right, I edited the question, the error as you said, the last error comes from another echo! – MK Said Oct 11 '22 at 12:28
  • 1
    P.S. As an aside, your variable naming is back to front! You've called the raw JSON $array, when it's actually a string (containing JSON-encoded text), and you've called the decoded data $json, when the action which created that variable was to convert it _from_ JSON into a PHP variable, so it has at that point ceased to be JSON! While it's a small thing on the surface, if you give your variables misleading names it makes it harder for people to read and understand your code (and "people" includes you yourself when you come back to look at it in future to make changes or fix bugs!). – ADyson Oct 11 '22 at 12:31
  • 1
    these 3 lines do what you need $array = json_decode($json, true, 512, JSON_THROW_ON_ERROR); $vars_value = $array['Results']['Info'][0]; extract($vars_value); – Roberto Braga Oct 11 '22 at 12:42
  • 1
    Hope this will help: https://3v4l.org/fXrbej (I have added some explanation as well) – Alive to die - Anant Oct 11 '22 at 12:53
  • @ADyson thanks for solution and explanation it works with me with your solution – MK Said Oct 12 '22 at 08:05

0 Answers0