Shez Ratnani
Shez Ratnani

Reputation: 312

how to read a php json decoded array

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.

Upvotes: 0

Views: 36

Answers (2)

Gleeson88
Gleeson88

Reputation: 46

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'];

Upvotes: 1

chishiki
chishiki

Reputation: 702

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

Upvotes: 0

Related Questions