0

I'm trying to retrieve team1 score however i cant seem to figure out how to output this. So far i've got this to work where $obj is the json output.

$obj->recent

JSON

"recent": [
    [
        {
        "match_id": "64886",
        "has_vods": false,
        "game": "dota2",
        "team 1": {
            "score": "",
            "name": "Wheel Whreck While Whistling",
            "bet": "7%"
        },
        "team 2": {
            "score": "",
            "name": "Evil Geniuses DotA2",
            "bet": "68%"
        },
        "live in": "1m 42s",
        "title": "Wheel Whreck... 7% vs 68% Evil...",
        "url": "",
        "tounament": "",
        "simple_title": "Wheel Whreck... vs Evil...",
        "streams": []
        }
]
2
  • $obj = json_decode($json); ?! Commented Feb 24, 2015 at 0:28
  • 3
    $obj->recent[0][0]->{'team 1'}->score ? Commented Feb 24, 2015 at 0:29

2 Answers 2

2

You need to use json_decode(); This function returns proper object with arrays and objects inside. Now you need check what is an object and what is an array.

$obj = json_decode($obj, true);
$obj->recent; //array 
$obj->recent[0]; //first element of array
$obj->recent[0][0]; //first element of second array
$obj->recent[0][0]->{'team 1'}; //access to object team 1
$obj->recent[0][0]->{'team 1'}->score; //access to property of object team 1

You can find this helpful to understand what happens;

You can also check example on json_decode documentation

If you use var_dump function on $obj it will show you what is an array and what is an object.

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

Comments

0

You'll want to use json_decode to get that into an array. It looks like recent is an array of arrays of objects. So, you'll do something like

$json = json_decode($obj->recent, true);
$team1 = $json[0][0]['team 1']; //should return array
$score = $team1['score']

edit: Thanks for the comment, was missing a true as the second param in json_decode

1 Comment

if you don't pass true as the second parameter to json_decode the result is an object, not an array and this will throw errors.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.