1

I've managed to decode and echo a JSON feed. After running this command

print_r(json_decode($data,true));

this is what I see on the screen:

Array
(
  [sportId] => 29
  [last] => 96466864
  [league] => Array
    (
        [0] => Array
            (
                [id] => 1980
                [events] => Array
                    (
                        [0] => Array
                            (
                                [id] => 667177156
                                [starts] => 2016-11-26T15:00:00Z
                                [home] => Hull City
                                [away] => W.B.A
                                [rotNum] => 2504
                                [liveStatus] => 1
                                [status] => O
                                [parlayRestriction] => 2
                            )
                        [1] => Array
                            (
                                [id] => 672139467
                                [starts] => 2016-12-10T15:00:00Z
                                [home] => Hull City
                                [away] => Crystal Palace
                                [rotNum] => 2510
                                [liveStatus] => 1
                                [status] => O
                                [parlayRestriction] => 2
                            )
                        [2] => Array
                            (
                                [id] => 676973849
                                [starts] => 2016-12-26T15:00:00Z
                                [home] => Burnley
                                [away] => Middlesbrough
                                [rotNum] => 2519
                                [liveStatus] => 1
                                [status] => O
                                [parlayRestriction] => 2
                            )
                        )
                )
        )
)

I need to be able to use foreach to go through each [events] in this associative array, and to be able to get the result such as this:

Hull City v W.B.A.
Hull City v Crystal Palace
Burnley v Middlesbrough

I think everything is already parsed correctly and now it's just a matter of using the correct syntax to echo the result from the associative array, which I cannot do myself.

1
  • yes. parsing of json is all done - that's what json_decode does. now, you just got a plain old, boring, simple array. containing nested arrays. up to the array called $decoded['league'][0]['events'], over which you have to iterate. and then, each of those items is an array itself, which you can use like every other array. as in: $line['home'] Commented Jan 31, 2017 at 17:24

2 Answers 2

3

You can try this:

$data=json_decode($data,true);//converts in array

    foreach($data['league'] as $key=>$val){// this can be ommited if only 0 index is there after 
//league and $data['league'][0]['events'] can be used in below foreach instead of $val['events'].
      foreach($val['events'] as $keys=>$value){
        echo $value['home'].' v '.$value['away'].'<br>;
    }  
    }
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks buddy. Does exactly what I wanted.
1

Try like this..

$data=json_decode($data,true);//convert your json into array
$events = $data['leage'][0]['events'];//events array

foreach($events as $key=>$value)//loop inside your events array
{
  echo $value['home'].' v '.$value['away'].'<br>;
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.