【问题标题】:how to access stdclass object inside stdclass object in codeigniter如何在codeigniter中访问stdclass对象内的stdclass对象
【发布时间】: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


【解决方案1】:

避免使用属于数据类型的变量名。它们包含的内容更有用。当变量还没有用时,$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;
}

【讨论】:

  • $footballers 返回Array ( [callum-paterson] => { "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" },这是意料之外的。我想要 id 的值为 1.
  • 使用这个,你可以使用 `foreach ($footballers as $footballer) { echo $footballer->id; } 以返回 ID,但还有其他问题,我在您的帖子中提出了问题。
【解决方案2】:

我解决了我自己的问题,这是我的解决方案:-

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 包含所有的 unique_id。像这样你可以打印所有的值。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-22
    • 2013-10-17
    • 2020-06-28
    • 2010-12-06
    • 2010-11-22
    • 2012-06-24
    相关资源
    最近更新 更多