0

I get the following string when I use $JSON = var_dump(json_decode($MyJSON, true)); If I echo $JSON:-

array(1) {
["test"]=> array(2) {
    [0]=> array(3) {
        ["subject"]=> array(2) {
            ["type"]=> string(10) "blank node" ["val"]=> string(4) "_:b0" 
        } 
        ["predicate"]=> array(2) {
            ["type"]=> string(3) "IRI" ["val"]=> string(47) "http://www.w3.org/1999/02/22-rdf-syntax-ns#type" 
        }
        ["object"]=> array(2) {
            ["type"]=> string(3) "IRI" ["val"]=> string(25) "http://schema.org/WebPage" 
        }
    }
    [1]=> array(3) {
        ["subject"]=> array(2) {
            ["type"]=> string(10) "blank node" ["val"]=> string(4) "_:b0" 
        }
        ["predicate"]=> array(2) {
            ["type"]=> string(3) "IRI" ["val"]=> string(28) "http://schema.org/breadcrumb" 
        }
        ["object"]=> array(2) {
            ["type"]=> string(10) "blank node" ["val"]=> string(4) "_:b1" 
        }
    } 
 }
}

what if I want to echo $JSON->test[0]->predicate->val what is the right syntax? Sorry I am a beginner. It would be a big help. Thanks in advance.

4
  • I guess var_dump is not needed here... Commented May 29, 2018 at 22:53
  • try $JSON['test'][0]['predicate']['val'] Commented May 29, 2018 at 22:55
  • Read the documentation to find out what var_dump(json_decode(..., true)) actually does. Commented May 29, 2018 at 22:55
  • @PhilCross I tried echo $JSON["test"][0]["predicate"]["val"]; It prints the whole data I mean same output as above Commented May 29, 2018 at 22:59

2 Answers 2

1

Remove var_dump

$JSON = var_dump(json_decode($MyJSON, true));

So $JSON becomes

$JSON = json_decode($MyJSON, true);

You will then be able to access the variable required via

$JSON['test'][0]['predicate']['val'];
Sign up to request clarification or add additional context in comments.

Comments

0

Did you already try to do json_decode($MyJson) without true as second parameter? This will return a STD object which should resemble the way you want to access the json object. See the PHP documentation for more info (second parameter $assoc=true means that it will be converted to an associative array): json_decode

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.