0

My JSON file is like this and I want to read this file in PHP:

{
    "data": [
        {
            "time": 1383458400,
            "summary": "Mostly cloudy throughout the day.",        
        },
        {
            "time": 1383458400,
            "summary": "Mostly cloudy throughout the day.",        
        }
    ],
    "data": [
        {
            "time": 1383458400,
            "summary": "Mostly cloudy throughout the day.",        
        },
        {
            "time": 1383458400,
            "summary": "Mostly cloudy throughout the day.",        
        }
    ]
}

How can I read this file in PHP? I am using the following to read:

$str = file_get_contents('files/filenameme.json');
$json = json_decode($str, true); 
3
  • 1
    What is the error/problem you're facing ? Commented Apr 11, 2016 at 13:22
  • Your actual question is probably "how to traverse an array". If so, show what you tried, or explain your thoughts/difficulty to come up with an approach. Commented Apr 11, 2016 at 13:29
  • i can read only first part of json file not other like "data": [ { "time": 1383458400, "summary": "Mostly cloudy throughout the day.", }, { "time": 1383458400, "summary": "Mostly cloudy throughout the day.", } ] but other json file i am unable to read Commented Apr 11, 2016 at 13:30

1 Answer 1

1

Did you write the JSON by yourself?

You have two problems there: the "data" is a duplicate, and this should be unique like "data1", "data2". The other problem is the comma after the summary part -- it doesn't belong there.

If you fix these issues you should be good to go.

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

2 Comments

Both fixed how to get output using foreach?
You have an array within an array there. So if you use for each, you'll go in the data part, after that you need to select the other arrays within in the data part. foreach ( $json as $item ) { foreach ( $item as $value ) { echo $value['time']; echo $value['summary']; } }

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.