簡體   English   中英

如何在Codeigniter中訪問stdclass對象內的stdclass對象

[英]how to access stdclass object inside stdclass object in codeigniter

在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(例如[callum-paterson])中打印所有記錄(例如id,unique_id,名稱等)。

我是JSON的新手。 所以我做不到。 任何幫助將不勝感激。 我試過了foreach,但是沒有用。

避免使用作為數據類型的變量名。 它們包含的內容更有用。 當變量尚不可用時,可以使用$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;
}

我解決了自己的問題,這是我的解決方案:-

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。 這樣,您可以打印所有值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM