0

I'm trying to decode json file using json_decode(), with second parameter true after the string of the json that i got from file_get_contents(). Like below.

json_decode(file_get_contents($dir), true);

I already make sure that the directory in file is correct and stored at $dir the json file is below.

{
"must": {
    "title": {
        "tag": "<!--section title-->",
        "content": null,
        "with-content": true
    },
    "class": {
        "tag": "<!--section class-->",
        "content": null,
        "with-content": true
    }
}

But when i try to fetch the json file using the script below.

if(is_file_ready($dir)) {
    $set = json_decode(file_get_contents($dir), true);
    echo file_get_contents($dir);   

    if(isset($set['must'])) {
        foreach($set['must'] as $node) {
        echo $node['tag'];
    }
}

It shows only the first echo like below, which the echo $node['tag'] shows nothing.

{ "must": { "title": { "tag": "", "content": null, "with-content": true }, "class": { "tag": "", "content": null, "with-content": true } } }

When i change the echo $node['tag'] to print_r($node['tag']) it shows like this.

Array ( [tag] => [content] => [with-content] => 1 ) Array ( [tag] => [content] => [with-content] => 1 )

My question is why the tag returning null and the content too? are <!-- --> is readed as the comment at json same as html? or maybe something that i did wrong?

Thanks for any correction.

1 Answer 1

1

Your code works fine. You just try to run it in your browser. If you open the dev tools (f12) you will see that in the html the values are displayed as comments so you don't see them.

If you use postman to run your script you will see that the output is printed normally.

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

Yup, you right my code are working fine, cause it became comment so i can't see it in normal browser, thanks for your explanation.

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.