【问题标题】:How to get data from object having array in php如何从php中具有数组的对象获取数据
【发布时间】:2016-10-15 23:04:53
【问题描述】:

使用 var_export 函数后我有这样的数据

Google_Service_Drive_FileList::__set_state(array(
    'collection_key' => 'items',
    'internal_gapi_mappings' => array() ,
    'etag' => NULL,
    'itemsType' => 'Google_Service_Drive_DriveFile',
    'itemsDataType' => 'array',
    'kind' => NULL,
    'nextLink' => NULL,
    'nextPageToken' => NULL,
    'selfLink' => NULL,
    'modelData' => array(
        'items' => array(
            0 => array(
                'id' => '1dOiZFO6nFEYKSLnMRS9mKZPXFBjztg_BwgEmNOPC2tQ',
                'title' => 'Php zend download',
                 )
) ,
    ) ,
    'processed' => array() ,
))

我如何从中获取数据。 我正在尝试将其存储在变量$response

print_r($response->modelData['items']['0']['id']);

但一无所获。

【问题讨论】:

  • 现在我尝试了print_r($response['0']['modelData']['items']['0']['id']); 并得到了Notice: Undefined index: items
  • minimal reproducible example? $response 是从哪里来的?当您尝试print_r ($response) 时会发生什么?
  • $response来自文件列表api的响应

标签: php arrays object multidimensional-array


【解决方案1】:

选择它为

$response['items'][0]['id']

【讨论】:

  • 这行不通。您应该使用整数值作为数值数组的键。 [0],但不是 ['0']
  • 是的。我已经纠正了。早些时候从问题部分复制。感谢您的评论。
【解决方案2】:

我尝试了很多东西,但没有奏效。 但现在我删除了var_export 并使用了$response['modelData']['items']['0']['id'];,其中$response 是一个对象类型,它工作并给了我id 的值。

当我只打印print_r($response) 时:

Google_Service_Drive_FileListObject([collection_key:
    protected ] => items[internal_gapi_mappings:
        protected ] => Array() [etag] => [itemsType:
            protected ] => Google_Service_Drive_DriveFile[itemsDataType:
                protected ] => array [kind] => [nextLink] => [nextPageToken] => [selfLink] => [modelData:
                    protected ] => Array(
                        [items] => Array(
                            [0] => Array(
                                [id] => 0BzwXST - A2MuCY0FOd3ZNUVJQYk0[webContentLink] => https: //docs.google.com/uc?id=0BzwXST-A2MuCY0FOd3ZNUVJQYk0&export=download [title] => aucti.pdf )
                                ) ,
                            ) ,
                            'processed' => array() ,
                        ))

但是使用 var_export 我得到了:

Google_Service_Drive_FileList::__set_state(array(
    'collection_key' => 'items',
    'internal_gapi_mappings' => array() ,
    'etag' => NULL,
    'itemsType' => 'Google_Service_Drive_DriveFile',
    'itemsDataType' => 'array',
    'kind' => NULL,
    'nextLink' => NULL,
    'nextPageToken' => NULL,
    'selfLink' => NULL,
    'modelData' => array(
        'items' => array(
            0 => array(
                'id' => '0BzwXST-A2MuCY0FOd3ZNUVJQYk0',
                'webContentLink' => 'https://docs.google.com/uc?id=0BzwXST-A2MuCY0FOd3ZNUVJQYk0&export=download',
                'title' => 'aucti.pdf',
            ) ,
        ) ,
    ) ,
    'processed' => array() ,
))

【讨论】:

  • 那么你的问题回答了吗?
  • 不是我想要的方式,但我已经实现了我想要的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-04
  • 1970-01-01
  • 2020-07-20
  • 1970-01-01
  • 2019-06-05
  • 1970-01-01
相关资源
最近更新 更多