【发布时间】:2018-12-05 11:17:52
【问题描述】:
在 Cron 控制器中,我编写了以下函数:-
public function football() {
$jsonData = file_get_contents('http://localhost/football/football/api/football_api.php');
}
$jsonData is returning the following object:-
stdClass Object
(
[callum-paterson] => stdClass Object
(
[id] => 1
[unique_id] => callum-paterson
[name] => Callum Paterson
[club] => Cardiff City
[position] => Defender
[national_team_name] => Unknown
[birthdate] => 1994-10-13
[birthplace] => Unknown
[nationality] => Unknown
[preferred_foot] => Unknown
[weight] => 76
[height] => 183
[shirt_number] => 13
[real_position] => Full Back
[real_position_side] => Right
[country] => Scotland
[photo_url] => https://img.footballindex.co.uk/callum-paterson-g-t1.jpg
)
[chicharito] => stdClass Object
(
[id] => 3
[unique_id] => chicharito
[name] => Chicharito
[club] => West Ham United
[position] => Forward
[national_team_name] => Unknown
[birthdate] => 1988-06-01
[birthplace] => Guadalajara
[nationality] => Mexico
[preferred_foot] => Right
[weight] => 73
[height] => 175
[shirt_number] => 17
[real_position] => Striker
[real_position_side] => Centre
[country] => Mexico
[photo_url] => https://img.footballindex.co.uk/javier-hernandez-g-t6.jpg
)
$jsonData 返回 1064 条记录。这里我展示一些。所以我想打印 stdclass-object_index 中的所有记录(如 id、unique_id、name 等)(如 [callum-paterson] )。
我是 JSON 新手。所以我做不到。任何帮助将不胜感激。我尝试了 foreach 但它不起作用。
【问题讨论】:
-
在你的
foreach循环中使用json_decode- 看看@php.net/manual/en/function.json-decode.php -
+sintakonte 能给我举个例子吗??
-
在线下
file_get_contents,可以echo gettype($jsonData)吗?我不认为这是一个数组。 -
嘿,它没有以正确的 JSON 格式出现,所以我更新了 api。您现在可以查看更新的代码。
-
你需要告诉我们
gettype()是$jsonData的什么。
标签: php arrays json codeigniter