0

I am trying to extract team names, players name from this json. but get no resul.

My json file looks like. any idea where I am wrong ?

{"items":[
{"_id":305501,"id":"305501","created_at":"2015-10-19T19:57:02+13:00","updated_at":"2015-10-19T21:34:50+13:00","name":"Match in progress","verification_level":0,
    "club_one":{"name":"Degree Club","id":14748,
        "team":{"name":"Degree College XI","id":13009,"avatar":"/original/team/default_thumb.png",
            "players":[
            {"id":null,"name":"D Vinaya"},
            {"id":617744,"name":"V Avika"},
            {"id":617745,"name":"C Rumes"},                         
            {"id":1360372,"name":"R Ferdo"}
            ],
            "innings":[{"overs":34,"over_balls":0,"runs":99,"wickets":7}]
        }
    },
    "club_two":{"name":"George Club","id":147736,
        "team":{"name":"George College XI","id":154503,"avatar":"/original/team/default_thumb.png",
            "players":[
            {"id":null,"name":"M Premathe†"},
            {"id":null,"name":"S Tion"},
            {"id":null,"name":"N Perra"},               
            {"id":1400317,"name":"S Ren"}
            ],
            "innings":[]
        }
    },
"processed":true,"visible":true,"match_level":{"name":null,"id":null}   
}   
],
"meta":{"total_pages":1}
}

and here is the php code.

$json_file = json_decode($load_json);
           foreach ($items as $item) {                
               echo $item->$json_file->_id;
                echo $item->$json_file->club_one;
                echo $item->$json_file->club_two;
                   }
0

1 Answer 1

1

You are doing it wrong. As far as your json is concerned, loop it like this

$json_file = json_decode($json);
foreach ($json_file->items as $item) {                
    echo $item->id;
    echo $item->club_one->name;
    echo $item->club_two->name;
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for reply. I will check it later. How about getting team name of each club ? echo $item->club_two->team; will work ?
I don't know, probably for the level of the question asked. BTW as far as your question is concerned echo $item->club_two->team; so if your team is an array you can access it like echo $item->club_two['team']; or if it an object then the way you did already.
If my answer helped you, don't forget to up-vote accept the answer so other can benefit from the situation question.

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.