如何在php中从复杂对象中获取值

时间:2016-02-05 04:34:01

标签: php object

我正在使用Google Calendar API,试图从响应对象中获取ID。

这是对象。

Google_Service_Calendar_CalendarList Object
(
[collection_key:protected] => items
[internal_gapi_mappings:protected] => Array
    (
    )

[etag] => "1454629096373000"
[itemsType:protected] => Google_Service_Calendar_CalendarListEntry
[itemsDataType:protected] => array
[kind] => calendar#calendarList
[nextPageToken] => 
[nextSyncToken] => CIj2xtSj38oCEj1nY2FsL....
[modelData:protected] => Array
    (
        [items] => Array
            (
                [0] => Array
                    (
                        [kind] => calendar#calendarListEntry
                        [etag] => "145461934381000"
                        [id] => tbcv49j3eg@group.calendar.google.com
                        [summary] => Appointments
                        [timeZone] => America/Chicago
                        [colorId] => 24
                        [backgroundColor] => #a47ae2
                        [foregroundColor] => #000000
                        [selected] => 1
                        [accessRole] => reader
                        [defaultReminders] => Array
                            (
                            )

                    )

                [1] => Array
                    (
                        [kind] => calendar#calendarListEntry
                        [etag] => "14546290978873000"
                        [id] => o6lkfgdovcv2r8@group.calendar.google.com
                        [summary] => Test Calendar
                        [timeZone] => America/Chicago
                        [colorId] => 14
                        [backgroundColor] => #9fe1e7
                        [foregroundColor] => #000000
                        [selected] => 1
                        [accessRole] => reader
                        [defaultReminders] => Array
                            (
                            )

                    )

            )

    )

所以我这样做是为了让他们进入一个数组。

foreach ($calendarList['items'] as $key => $value) {
     $ids[] = $calendarList['items'][$key]['id'];
}

但是我想以更加面向对象的方式引用它们,比如

$calendarList->items->

我能以某种方式这样做吗?

2 个答案:

答案 0 :(得分:3)

$calendarListNew->items->{'1'}

所以你可以获取像:

这样的值
<html>
    <head>
        <title>Test Loading</title>
    </head>
    <body>
        <div id="header">
            This is header
        </div>
        <div id="navigation">
            This is navigation
        </div>
        <div id="content">
            <form action="test2.php" method="post">
                <table>
                    <tr>
                        <td>First Name</td>
                        <td>:</td>
                        <td><input type="text" name="first_name"></td>
                    </tr>
                    <tr>
                        <td>Last Name</td>
                        <td>:</td>
                        <td><input type="text" name="last_name"></td>
                    </tr>
                    <tr>
                        <td>Age</td>
                        <td>:</td>
                        <td><input type="text" name="age"></td>
                    </tr>
                    <tr>
                        <td>Hobby</td>
                        <td>:</td>
                        <td><input type="text" name="hobby"></td>
                    </tr>
                    <tr>
                        <td></td>
                        <td></td>
                        <td><input type="submit" Value="Submit"></td>
                    </tr>
                </table>

            </form>
        </div>
        <div id="footer">
            This is footer
        </div>
    </body>
</html>

答案 1 :(得分:0)

$value将拥有每个项目,您可以使用以下

foreach ($calendarList['items'] as $key => $value) {
    $value = (object) $value;
    $ids[] = $value->id;
}

How to convert an array into an object