-1

I have JSON output like :

{
date: "26-07-2018",
leagues: [
 {
   league_id: 0,
   league_name: "xxx",
   league_logo: "http://3.bp.blogspot.com/-Eczfs3_ibNw/V5QDDwyxP5I/AAAAAAAAEdM/Jry4L9hzJQwmnWr3Jf1Amy4vzdGfdeeFwCK4B/s60/INT_Champions_Cup_logo.png",
   league_matches: [
          {
            match_id: 1,
            team1logo: "http://1.bp.blogspot.com/-POxAfSSnlW0/UhCt7oQsdLI/AAAAAAAAGDo/rMRXx2mqvUI/s60/Juventus[1].Png",
           team2logo: "http://4.bp.blogspot.com/-8oH--7NYdUI/UgbIXYLMwiI/AAAAAAAAEQo/30KQWgGRJyg/s60/Bayern+Munchen+(2).png",
           match_time: "23:00",
           channels_id: [
                         1
                        ],
          team1: "xxx",
          team2: "xxx"
         },
        {
         match_id: 2,
         team1logo: "http://1.bp.blogspot.com/-86nRbJxtZ_o/UgbePFW0WEI/AAAAAAAAFFw/R6Gvxeyt1gQ/s60/Manchester+City+(2).png",
         team2logo: "http://3.bp.blogspot.com/-D0lb4b-qN5U/UgbeDosqjYI/AAAAAAAAFBY/Qg7kvoodvFY/s60/Liverpool+(2).png",
         match_time: "00:00",
         channels_id: [
                       2
                      ],
         team1: "xxx",
         team2: "xxx"
        }
      ]
    }
  ]
}

Here i can read date like :

$json = file_get_contents('URL');
$obj = json_decode($json);
echo $obj->date;    

But I need to read leagues and league_name and team1 ..... all of the element.

How can get it?

Can I use json_decode to parse each value for further data processing?

Thanks.

Gufran Hasan
  • 8,910
  • 7
  • 38
  • 51
  • Possible duplicate of [How do I extract data from JSON with PHP?](https://stackoverflow.com/questions/29308898/how-do-i-extract-data-from-json-with-php) – user3942918 Jul 27 '18 at 07:41
  • @paul ,, its not same my data ,, see my JSON ,,, its different –  Jul 27 '18 at 07:45

2 Answers2

1

you can try someting like

   $obj = json_decode($json, true);
   echo $obj['leagues']['league_name'];
   foreach($obj['leagues']['league_matches'] as $matches) {
       echo $matches['team1'];
   }    
0

Actually, your json response contains Date is in Object and Leage Name in an array and league_matches is an array of object.

 echo 'Date: '.$obj->date.'<br/>';
            $leagues = $obj->leagues;
            echo 'Leage Name: '.$leagues[0]->league_name.'<br/>';
           $team1 = '';
           foreach($leagues[0]->league_matches as $matches) {
          $team1 =$team1.','.$matches->team1;
          echo 'Team1: '.$matches->team1.'<br/>';
       }
//after concatenation of team1 with comma
echo 'Team1: '.ltrim($team1,',').'<br/>';

Output:

    Date: 26-07-2018
    Leage Name: xxx
    Team1: xxx
    Team1: xxx 
//after concatenation of team1 with comma
    Team1: xxx,xxx
Gufran Hasan
  • 8,910
  • 7
  • 38
  • 51