在PHP中打印多维数组的特定部分

问题描述 投票:0回答:1

我对编程和PHP完全陌生。我正在尝试将Google驱动器中的文件列表显示在我的PHP页面上。我已经能够获取Google驱动器上所有可用信息的详细数组,但我只需要特定信息。代码和数组的结构如下:

Code (based on Google Drive API):
    $results = $service->files->listFiles(); 
    print "<pre>";
    print_r ($results);
    print "</pre>";

Array Output:
Array
(
    [kind] => drive#fileList
    [etag] => "StLURsdfsdfasdfvFAD7aKbdasdfdsfasfads"
    [selfLink] => https://www.googleapis.com/drive/v2/files
    [incompleteSearch] => 
    [items] => Array
        (
            [0] => Array
                (
                    [kind] => drive#file
                    [id] => StLURsdfsdfasdfv
                    [etag] => " StLURsdfsdfasdfv"
                    [selfLink] => https://www.googleapis.com/drive/v2/ddewweewe
                    [webContentLink] => https://www.googleapis.com/drive/v2/ddewweewe
                    [alternateLink] => https://www.googleapis.com/drive/v2/ddewweewe
                    [embedLink] => https://www.googleapis.com/drive/v2/ddewweewe
                    [iconLink] => https://drive-thirdparty.googleusercontent.com/16/type/image/jpeg
                    [thumbnailLink] => https://www.googleapis.com/drive/v2/ddewweewe
                    [title] => clothing.jpg
                    [mimeType] => image/jpeg
                    [description] => This is a image/jpeg document
                    [labels] => Array
                        (
                            [starred] => 
                            [hidden] => 
                            [trashed] => 
                            [restricted] => 
                            [viewed] => 1
                        )

                    [copyRequiresWriterPermission] => 
                    [createdDate] => 2019-04-10T21:59:15.594Z
                    [modifiedDate] => 2019-04-10T21:59:15.594Z
                    [modifiedByMeDate] => 2019-04-10T21:59:15.594Z
                    [lastViewedByMeDate] => 2019-04-10T21:59:15.594Z
                    [markedViewedByMeDate] => 1970-01-01T00:00:00.000Z
                    [version] => 1
                    [parents] => Array
                        (
                            [0] => Array
                                (
                                    [kind] => drive#parentReference
                                    [id] => 0AJdsdasdasdsd
                                    [selfLink] => https://www.googleapis.com/drive/v2/ddewweewe
                                    [parentLink] => https://www.googleapis.com/drive/v2/ddewweewe
                                    [isRoot] => 1
                                )
                        )

                    [downloadUrl] => https://www.googleapis.com/drive/v2/ddewweewe
                    [userPermission] => Array
                        (
                            [kind] => drive#permission
                            [etag] => "sdadasdsadasdasd"
                            [id] => me
                            [selfLink] => https://www.googleapis.com/drive/v2/ddewweewe

                            [role] => owner
                            [type] => user
                        )

                    [originalFilename] => clothing.jpg
                    [fileExtension] => jpg
                    [md5Checksum] => etetetet
                    [fileSize] => 12162
                    [quotaBytesUsed] => 12162
                    [ownerNames] => Array
                        (
                            [0] => Jawad Khokhar
                        )

                    [owners] => Array
                        (
                            [0] => Array
                                (
                                    [kind] => drive#user
                                    [displayName] => Jawad Khokhar
                                    [isAuthenticatedUser] => 1
                                    [permissionId] => 035eere2323296
                                    [emailAddress] => [email protected]
                                )

                        )

                    [lastModifyingUserName] => Jawad Khokhar
                    [lastModifyingUser] => Array
                        (
                            [kind] => drive#user
                            [displayName] => Jawad Khokhar
                            [isAuthenticatser] => 1
                            [permissionId] => 566777
                            [emailAddress] => [email protected]
                        )

                    [capabilities] => Array
                        (
                            [canCopy] => 1
                            [canEdit] => 1
                        )

                    [editable] => 1
                    [copyable] => 1
                    [writersCanShare] => 1
                    [shared] => 
                    [explicitlyTrashed] => 
                    [appDataContents] => 
                    [headRevisionId] => 0B5u7riwJ5Uw66503jdmdkjkjwekjweYe
                    [imageMediaMetadata] => Array
                        (
                            [width] => 343
                            [height] => 147
                            [rotation] => 0
                        )

                    [spaces] => Array
                        (
                            [0] => drive
                        )
                )

            [1] => Array
                (
                    [kind] => drive#file
                    [id] => 1Bj160bjs9atttt4ttttt6kjnkj
                    [etag] => "tttttttttt"
                    [selfLink] => https://www.googleapis.com/drive/v2/ddewweewe
                    [webContentLink] => https://www.googleapis.com/drive/v2/ddewweewe
                    [alternateLink] => https://www.googleapis.com/drive/v2/ddewweewe

                    [embedLink] => https://www.googleapis.com/drive/v2/ddewweewe
                    [iconLink] => https://www.googleapis.com/drive/v2/ddewweewe
                    [thumbnailLink] => https://www.googleapis.com/drive/v2/ddewweewe

                    [title] => edu.jpg
                    [mimeType] => image/jpeg
                    [description] => This is a image/jpeg document
                    [labels] => Array
                        (
                            [starred] => 
                            [hidden] => 
                            [trashed] => 
                            [restricted] => 
                            [viewed] => 1
                        )

                    [copyRequiresWriterPermission] => 
                    [createdDate] => 2020-04-10T21:58:10.514Z
                    [modifiedDate] => 2020-04-10T21:58:10.514Z
                    [modifiedByMeDate] => 2020-04-10T21:58:10.514Z
                    [lastViewedByMeDate] => 2020-04-10T21:58:10.514Z
                    [markedViewedByMeDate] => 1970-01-01T00:00:00.000Z
                    [version] => 1
                    [parents] => Array
                        (
                            [0] => Array
                                (
                                    [kind] => drive#parentReference
                                    [id] => 0AJu7riwJ5UYSUk9PVA
                                    [selfLink] => https://www.googleapis.com/drive/v2/ddewweewe
                                    [parentLink] => https://www.googleapis.com/drive/v2/ddewweewe
                                    [isRoot] => 1
                                )
                        )

                    [downloadUrl] => https://www.googleapis.com/drive/v2/ddewweewe
                    [userPermission] => Array
                        (
                            [kind] => drive#permission
                            [etag] => "wefwefwefwefeffewfwef_FE"
                            [id] => me
                            [selfLink] => https://www.googleapis.com/drive/v2/ddewweewe
                            [role] => owner
                            [type] => user
                        )
                    [originalFilename] => edu.jpg
                    [fileExtension] => jpg
                    [md5Checksum] => 1c07f6b5da068asdaasdasdfc8f63753
                    [fileSize] => 16751
                    [quotaBytesUsed] => 16751
                    [ownerNames] => Array
                        (
                            [0] => Jawad Khokhar
                        )
                    [owners] => Array
                        (
                            [0] => Array
                                (
                                    [kind] => drive#user
                                    [displayName] => Jawad Khokhar
                                    [isAuthenticatedUser] => 1
                                    [permissionId] => 035361535352124241296
                                    [emailAddress] => [email protected]
                                )
                        )
                    [lastModifyingUserName] => Jawad Khokhar
                    [lastModifyingUser] => Array
                        (
                            [kind] => drive#user
                            [displayName] => Jawad Khokhar
                            [isAuthenticatedUser] => 1
                            [permissionId] => 0353655352124241296
                            [emailAddress] => [email protected]
                        )

                    [capabilities] => Array
                        (
                            [canCopy] => 1
                            [canEdit] => 1
                        )

                    [editable] => 1
                    [copyable] => 1
                    [writersCanShare] => 1
                    [shared] => 
                    [explicitlyTrashed] => 
                    [appDataContents] => 
                    [headRevisionId] => 0B5u7riwJ5UYSUlR0UkpqstsetFEeVhiZmxUei9FVys4K0NzPQ
                    [imageMediaMetadata] => Array
                        (
                            [width] => 237
                            [height] => 212
                            [rotation] => 0
                        )
                    [spaces] => Array
                        (
                            [0] => drive
                        )
                )
        )
)

这仅是两个文件的数据。我只需要在PHP中打印'[embedlink]'和'[title]'。

php arrays google-drive-api
1个回答
0
投票

您只需要遍历数组(foreach)并从每个文件中索取所需的索引。

foreach($your_unnamed_array_lets_call_it_google_drive_data['items'] as $file){
    echo $file['embedlink'];
    echo $file['title'];
    //or do anything else with them...
}
© www.soinside.com 2019 - 2024. All rights reserved.