PHP:从JSON获取元素
我的JSON输出如下:PHP:从JSON获取元素,php,json,Php,Json,我的JSON输出如下: { 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_ma
{
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"
}
]
}
]
}
在这里,我可以读到如下日期:
$json = file_get_contents('URL');
$obj = json_decode($json);
echo $obj->date;
但我需要阅读联赛和联赛的名称和球队1。。。。。所有的元素
你怎么能得到它
我可以使用json_decode解析每个值以进行进一步的数据处理吗
谢谢。你可以试试
$obj = json_decode($json, true);
echo $obj['leagues']['league_name'];
foreach($obj['leagues']['league_matches'] as $matches) {
echo $matches['team1'];
}
实际上,您的json响应包含
Date
在Object
中,Leage Name
在数组中,league\u matches
是对象的数组
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/>';
@paul可能重复,它与我的数据不同,请参阅我的JSON,它的different@abukotdhalmallahi,试试这个逻辑。
Date: 26-07-2018
Leage Name: xxx
Team1: xxx
Team1: xxx
//after concatenation of team1 with comma
Team1: xxx,xxx