[英]how to access stdclass object inside stdclass object in codeigniter
In the Cron controller I have written the following function:- 在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 is returning 1064 records. $ jsonData返回1064条记录。 Here I'm showing a few.
这里我展示了一些。 so I want to print all the records(like id,unique_id,name etc.) inside the stdclass-object_index(like [callum-paterson] ).
所以我想在stdclass-object_index(例如[callum-paterson])中打印所有记录(例如id,unique_id,名称等)。
I'm new to JSON. 我是JSON的新手。 So I cannot do it.
所以我做不到。 Any help will be appreciated.
任何帮助将不胜感激。 I tried foreach but it's not working.
我试过了foreach,但是没有用。
Avoid using variable names that are the type of data. 避免使用作为数据类型的变量名。 What they contain is more useful.
它们包含的内容更有用。
$data
is acceptable when the variable is not yet useful. 当变量尚不可用时,可以使用
$data
。
public function football() {
$footballers = file_get_contents('http://localhost/football/football/api/football_api.php');
foreach ($footballers as $footballer => $data) {
$footballers[$footballer] = json_decode($data);
}
return $footballers;
}
I solved my own problem here is my solution:- 我解决了自己的问题,这是我的解决方案:-
public function football() {
header('Content-Type: application/json');
$tmp = array();
$footballers = json_decode(file_get_contents('http://localhost/football/football/api/football_api.php'));
foreach ($footballers as $footballer => $data) {
$tmp[] = $data->unique_id;
}
print_r($tmp);
}
$tmp holds all the unique_ids. $ tmp保存所有unique_id。 Like that You can print all the values.
这样,您可以打印所有值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.