Dan Horvat
Dan Horvat

Reputation: 796

Parse JSON with PHP using foreach

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.

Upvotes: 1

Views: 16210

Answers (2)

Hikmat Sijapati
Hikmat Sijapati

Reputation: 6994

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>;
}

Upvotes: 1

Suchit kumar
Suchit kumar

Reputation: 11859

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>;
    }  
    }

Upvotes: 3

Related Questions