无法访问foreach中的json位置

时间:2015-08-05 13:22:26

标签: php json

我正在尝试获取此JSON的所有值:

{"_links":[{"self":"http://api.football-data.org/alpha/soccerseasons/394/teams"},{"soccerseason":"http://api.football-data.org/alpha/soccerseasons/394"}],"count":18,"teams":[{"_links":{"self":{"href":"http://api.football-data.org/alpha/teams/5"},"fixtures":{"href":"http://api.football-data.org/alpha/teams/5/fixtures"},"players":{"href":"http://api.football-data.org/alpha/teams/5/players"}},"name":"FC Bayern München","code":"FCB","shortName":"Bayern","squadMarketValue":"551,250,000 €","crestUrl":"http://upload.wikimedia.org/wikipedia/commons/c/c5/Logo_FC_Bayern_München.svg"},{"_links":{"self":{"href":"http://api.football-data.org/alpha/teams/7"},"fixtures":{"href":"http://api.football-data.org/alpha/teams/7/fixtures"},"players":{"href":"http://api.football-data.org/alpha/teams/7/players"}},"name":"Hamburger SV","code":"HSV","shortName":"HSV","squadMarketValue":"71,100,000 €","crestUrl":"http://upload.wikimedia.org/wikipedia/commons/6/66/HSV-Logo.svg"},{"_links":{"self":{"href":"http://api.football-data.org/alpha/teams/16"},"fixtures":{"href":"http://api.football-data.org/alpha/teams/16/fixtures"},"players":{"href":"http://api.football-data.org/alpha/teams/16/players"}},"name":"FC Augsburg","code":"FCA","shortName":"Augsburg","squadMarketValue":"48,550,000 €","crestUrl":"http://upload.wikimedia.org/wikipedia/de/b/b5/Logo_FC_Augsburg.svg"},{"_links":{"self":{"href":"http://api.football-data.org/alpha/teams/9"},"fixtures":{"href":"http://api.football-data.org/alpha/teams/9/fixtures"},"players":{"href":"http://api.football-data.org/alpha/teams/9/players"}},"name":"Hertha BSC","code":"BSC","shortName":"Hertha","squadMarketValue":"63,700,000 €","crestUrl":"http://upload.wikimedia.org/wikipedia/de/8/81/Hertha_BSC_Logo_2012.svg"},{"_links":{"self":{"href":"http://api.football-data.org/alpha/teams/3"},"fixtures":{"href":"http://api.football-data.org/alpha/teams/3/fixtures"},"players":{"href":"http://api.football-data.org/alpha/teams/3/players"}},"name":"Bayer Leverkusen","code":"B04","shortName":"Leverkusen","squadMarketValue":"177,100,000 €","crestUrl":"http://upload.wikimedia.org/wikipedia/de/9/95/Bayer_04_Leverkusen_Logo.svg"},{"_links":{"self":{"href":"http://api.football-data.org/alpha/teams/2"},"fixtures":{"href":"http://api.football-data.org/alpha/teams/2/fixtures"},"players":{"href":"http://api.football-data.org/alpha/teams/2/players"}},"name":"TSG 1899 Hoffenheim","code":"TSG","shortName":"Hopenhoam","squadMarketValue":"118,200,000 €","crestUrl":"http://upload.wikimedia.org/wikipedia/de/e/e7/Logo_TSG_Hoffenheim.svg"},{"_links":{"self":{"href":"http://api.football-data.org/alpha/teams/55"},"fixtures":{"href":"http://api.football-data.org/alpha/teams/55/fixtures"},"players":{"href":"http://api.football-data.org/alpha/teams/55/players"}},"name":"SV Darmstadt 98","code":"DAR","shortName":"Darmstadt","squadMarketValue":"12,450,000 €","crestUrl":"http://upload.wikimedia.org/wikipedia/de/8/87/Svdarmstadt98.svg"},{"_links":{"self":{"href":"http://api.football-data.org/alpha/teams/8"},"fixtures":{"href":"http://api.football-data.org/alpha/teams/8/fixtures"},"players":{"href":"http://api.football-data.org/alpha/teams/8/players"}},"name":"Hannover 96","code":"H96","shortName":"Hannover","squadMarketValue":"74,500,000 €","crestUrl":"http://upload.wikimedia.org/wikipedia/de/c/cd/Hannover_96_Logo.svg"},{"_links":{"self":{"href":"http://api.football-data.org/alpha/teams/15"},"fixtures":{"href":"http://api.football-data.org/alpha/teams/15/fixtures"},"players":{"href":"http://api.football-data.org/alpha/teams/15/players"}},"name":"1. FSV Mainz 05","code":"M05","shortName":"Mainz","squadMarketValue":"75,200,000 €","crestUrl":"http://upload.wikimedia.org/wikipedia/de/0/0b/FSV_Mainz_05_Logo.svg"},{"_links":{"self":{"href":"http://api.football-data.org/alpha/teams/31"},"fixtures":{"href":"http://api.football-data.org/alpha/teams/31/fixtures"},"players":{"href":"http://api.football-data.org/alpha/teams/31/players"}},"name":"FC Ingolstadt 04","code":"FCI","shortName":"Ingolstadt","squadMarketValue":"18,600,000 €","crestUrl":"http://upload.wikimedia.org/wikipedia/de/5/55/FC-Ingolstadt_logo.svg"},{"_links":{"self":{"href":"http://api.football-data.org/alpha/teams/12"},"fixtures":{"href":"http://api.football-data.org/alpha/teams/12/fixtures"},"players":{"href":"http://api.football-data.org/alpha/teams/12/players"}},"name":"Werder Bremen","code":"SVW","shortName":"Bremen","squadMarketValue":"52,600,000 €","crestUrl":"http://upload.wikimedia.org/wikipedia/commons/b/be/SV-Werder-Bremen-Logo.svg"},{"_links":{"self":{"href":"http://api.football-data.org/alpha/teams/6"},"fixtures":{"href":"http://api.football-data.org/alpha/teams/6/fixtures"},"players":{"href":"http://api.football-data.org/alpha/teams/6/players"}},"name":"FC Schalke 04","code":"S04","shortName":"Schalke","squadMarketValue":"208,850,000 €","crestUrl":"http://upload.wikimedia.org/wikipedia/de/6/6d/FC_Schalke_04_Logo.svg"},{"_links":{"self":{"href":"http://api.football-data.org/alpha/teams/4"},"fixtures":{"href":"http://api.football-data.org/alpha/teams/4/fixtures"},"players":{"href":"http://api.football-data.org/alpha/teams/4/players"}},"name":"Borussia Dortmund","code":"BVB","shortName":"Dortmund","squadMarketValue":"317,800,000 €","crestUrl":"http://upload.wikimedia.org/wikipedia/commons/6/67/Borussia_Dortmund_logo.svg"},{"_links":{"self":{"href":"http://api.football-data.org/alpha/teams/18"},"fixtures":{"href":"http://api.football-data.org/alpha/teams/18/fixtures"},"players":{"href":"http://api.football-data.org/alpha/teams/18/players"}},"name":"Bor. Mönchengladbach","code":"BMG","shortName":"M'gladbach","squadMarketValue":"130,450,000 €","crestUrl":"http://upload.wikimedia.org/wikipedia/de/c/cc/Borussia_Moenchengladbach_Logo.svg"},{"_links":{"self":{"href":"http://api.football-data.org/alpha/teams/11"},"fixtures":{"href":"http://api.football-data.org/alpha/teams/11/fixtures"},"players":{"href":"http://api.football-data.org/alpha/teams/11/players"}},"name":"VfL Wolfsburg","code":"WOB","shortName":"Wolfsburg","squadMarketValue":"206,350,000 €","crestUrl":"http://upload.wikimedia.org/wikipedia/de/b/bc/VfL_Wolfsburg_Logo_weiß.svg"},{"_links":{"self":{"href":"http://api.football-data.org/alpha/teams/19"},"fixtures":{"href":"http://api.football-data.org/alpha/teams/19/fixtures"},"players":{"href":"http://api.football-data.org/alpha/teams/19/players"}},"name":"Eintracht Frankfurt","code":"SGE","shortName":"Eintr. Frankfurt","squadMarketValue":"69,050,000 €","crestUrl":"http://upload.wikimedia.org/wikipedia/commons/0/04/Eintracht_Frankfurt_Logo.svg"},{"_links":{"self":{"href":"http://api.football-data.org/alpha/teams/10"},"fixtures":{"href":"http://api.football-data.org/alpha/teams/10/fixtures"},"players":{"href":"http://api.football-data.org/alpha/teams/10/players"}},"name":"VfB Stuttgart","code":"VFB","shortName":"Stuttgart","squadMarketValue":"89,050,000 €","crestUrl":"http://upload.wikimedia.org/wikipedia/commons/a/ab/VfB_Stuttgart_Logo.svg"},{"_links":{"self":{"href":"http://api.football-data.org/alpha/teams/1"},"fixtures":{"href":"http://api.football-data.org/alpha/teams/1/fixtures"},"players":{"href":"http://api.football-data.org/alpha/teams/1/players"}},"name":"1. FC Köln","code":"EFFZEH","shortName":"Köln","squadMarketValue":"42,150,000 €","crestUrl":"http://upload.wikimedia.org/wikipedia/de/1/16/1._FC_Köln.svg"}]}

我已经创建了这个解码器

$decoded = json_decode($response,true);

所以我执行foreach迭代对象:

foreach($decoded['teams'] as $team => $value)
{   
    var_dump($decoded['_links']['self']['href');
}

但此代码返回一个NULL对象。我想得到这个内容:

{"_links":[{"self":"http://api.football-data.org/alpha/soccerseasons/394/teams"},

json结构:

{
"_links": {
     "self": { "href": "http://api.football-data.org/alpha/teams/19" },
     "fixtures": { "href": "http://api.football-data.org/alpha/teams/19/fixtures" },
     "players": { "href": "http://api.football-data.org/alpha/teams/19/players" }
   },
   "name": "Eintracht Frankfurt",
   "code": "SGE",
   "shortName": "Eintr. Frankfurt",
   "squadMarketValue": "75.475.000 ?",
   "crestUrl": "http://upload.wikimedia.org/wikipedia/commons/0/04/Eintracht_Frankfurt_Logo.svg"
}

我做错了什么?

2 个答案:

答案 0 :(得分:1)

根据您的评论,我们可以看到

array(3) { 
    ["_links"]=> array(2) { 
        [0]=> array(1) { 
            ["self"]=> string(58) "api.football-data.org/alpha/soccerseasons/394/teams"; } 
        [1]=> array(1) { 
            ["soccerseason"]=> string(52) "api.football-...";}
    }

所以,为了得到你的自我"值,您必须访问$decoded["_links"][0]["self"]

答案 1 :(得分:0)

您使用$decoded var代替$value。它应该是这样的:

foreach($decoded['teams'] as $team => $value)
{   
    var_dump($value['_links']['self']['href']);
}


更新 要获得json的自我链接,您不需要迭代团队。你应该像这样迭代_links:

foreach($decoded['_links'] as $link)
{   
    var_dump(reset($link));
}

要获得第一个链接(仅当文档结构永远不会改变时),您只需要一行:

var_dump($decoded['_links'][0]['self']);