我遇到了一个从谷歌php api客户端返回的对象有问题。我试过使用该标签发布但没有帮助。我已经将对象转换为数组,但仍然无法弄清楚如何获取我需要的所有数据。 基本上我正在使用谷歌api进行搜索,它返回一个对象,结果如下(使用print_r):
Google_Service_Customsearch_Search Object (
[collection_key:protected] => promotions
[internal_gapi_mappings:protected] => Array ( )
[contextType:protected] => Google_Service_Customsearch_Context
[contextDataType:protected] =>
[itemsType:protected] => Google_Service_Customsearch_Result
[itemsDataType:protected] =>
array [kind] => customsearch#search
[promotionsType:protected] => Google_Service_Customsearch_Promotion
[promotionsDataType:protected] => array
[queriesType:protected] => Google_Service_Customsearch_Query
[queriesDataType:protected] => map
[searchInformationType:protected] => Google_Service_Customsearch_SearchSearchInformation
[searchInformationDataType:protected] =>
[spellingType:protected] => Google_Service_Customsearch_SearchSpelling
[spellingDataType:protected] =>
[urlType:protected] => Google_Service_Customsearch_SearchUrl
[urlDataType:protected] =>
[modelData:protected] => Array (
[url] => Array (
[type] => application/json
[template] => https://www.googleapis.com/customsearch/v1?q={searchTerms}&num={count?}&start={startIndex?}&lr={language?}&safe={safe?}&cx={cx?}&cref={cref?}&sort={sort?}&filter={filter?}&gl={gl?}&cr={cr?}&googlehost={googleHost?}&c2coff={disableCnTwTranslation?}&hq={hq?}&hl={hl?}&siteSearch={siteSearch?}&siteSearchFilter={siteSearchFilter?}&exactTerms={exactTerms?}&excludeTerms={excludeTerms?}&linkSite={linkSite?}&orTerms={orTerms?}&relatedSite={relatedSite?}&dateRestrict={dateRestrict?}&lowRange={lowRange?}&highRange={highRange?}&searchType={searchType}&fileType={fileType?}&rights={rights?}&imgSize={imgSize?}&imgType={imgType?}&imgColorType={imgColorType?}&imgDominantColor={imgDominantColor?}&alt=json
)
[queries] => Array (
[nextPage] => Array (
[0] => Array (
[title] => Google Custom Search - donkey
[totalResults] => 101000000
[searchTerms] => donkey
[count] => 5
[startIndex] => 6
[inputEncoding] => utf8
[outputEncoding] => utf8
[safe] => medium
[cx] => 000701869894579992216:cobbhbhbhbhre0
[searchType] => image
[imgSize] => large )
)
[request] => Array (
[0] => Array (
[title] => Google Custom Search - donkey
[totalResults] => 101000000
[searchTerms] => donkey
[count] => 5
[startIndex] => 1
[inputEncoding] => utf8
[outputEncoding] => utf8
[safe] => medium
....
)
)
)
)
[processed:protected] => Array ( )
)
我在检索我想要的值时遇到问题,因为有些项目缺少值,第一个是[contextDataType:protected] => 这会导致各种各样的问题,我不确定为什么会像这样返回对象。
所以我转换成了一个数组: $ array =(array)$ results; 这给出了:
Array ( [*collection_key] => promotions [*internal_gapi_mappings] => Array ( ) [*contextType] => Google_Service_Customsearch_Context [*contextDataType] => [*itemsType] => Google_Service_Customsearch_Result [*itemsDataType] => array [kind] => customsearch#search [*promotionsType] => Google_Service_Customsearch_Promotion [*promotionsDataType] => array [*queriesType] => Google_Service_Customsearch_Query [*queriesDataType] => map [*searchInformationType] => Google_Service_Customsearch_SearchSearchInformation [*searchInformationDataType] => [*spellingType] => Google_Service_Customsearch_SearchSpelling [*spellingDataType] => [*urlType] => Google_Service_Customsearch_SearchUrl [*urlDataType] => [*modelData] => Array ( [url] => Array ( [type] => application/json [template] => https://www.googleapis.com/customsearch/v1?q={searchTerms}&num={count?}&start={startIndex?}&lr={language?}&safe={safe?}&cx={cx?}&cref={cref?}&sort={sort?}&filter={filter?}&gl={gl?}&cr={cr?}&googlehost={googleHost?}&c2coff={disableCnTwTranslation?}&hq={hq?}&hl={hl?}&siteSearch={siteSearch?}&siteSearchFilter={siteSearchFilter?}&exactTerms={exactTerms?}&excludeTerms={excludeTerms?}&linkSite={linkSite?}&orTerms={orTerms?}&relatedSite={relatedSite?}&dateRestrict={dateRestrict?}&lowRange={lowRange?}&highRange={highRange?}&searchType={searchType}&fileType={fileType?}&rights={rights?}&imgSize={imgSize?}&imgType={imgType?}&imgColorType={imgColorType?}&imgDominantColor={imgDominantColor?}&alt=json ) [queries] => Array ( [nextPage] => Array ( [0] => Array ( [title] => Google Custom Search - donkey [totalResults] => 101000000 [searchTerms] => donkey [count] => 5 [startIndex] => 6 [inputEncoding] => utf8 [outputEncoding] => utf8 [safe] => medium [cx] => 000701869894579992216:coichdjqre0 [searchType] => image [imgSize] => large ) ) [request] => Array ( [0] => Array ( [title] => Google Custom Search - donkey [totalResults] => 101000000 [searchTerms] => donkey [count] => 5 [startIndex] => 1 [inputEncoding] => utf8 [outputEncoding] => utf8 [safe] => medium [cx] => 000701869894579992216:coichdjqre0 [searchType] => image [imgSize] => large ) ) ) [context] => Array ( [title] => Barebones Image Search ) [searchInformation] => Array ( [searchTime] => 0.241503 [formattedSearchTime] => 0.24 [totalResults] => 101000000 [formattedTotalResults] => 101,000,000 ) [items] => Array ( [0] => Array ( [kind] => customsearch#result [title] => Donkey - Wikipedia, the free encyclopedia [htmlTitle] => Donkey - Wikipedia, the free encyclopedia [link] => https://upload.wikimedia.org/wikipedia/commons/7/7b/Donkey_1_arp_750px.jpg [displayLink] => en.wikipedia.org [snippet] => Donkey [htmlSnippet] => Donkey [mime] => image/jpeg [image] => Array ( [contextLink] => https://en.wikipedia.org/wiki/Donkey [height] => 536 [width] => 750 [byteSize] => 185637 [thumbnailLink] => https://encrypted-tbn2.gstatic.com/images?q=tbn:ANd9GcSQDJFjmFAxjataSZZVvznpqrVKeHzsaVuc8hVxZFRrbjTphJjEsA-qdpA [thumbnailHeight] => 101 [thumbnailWidth] => 141 ) ) [1] => Array ( [kind] => customsearch#result [title] => Paradox Resolved: Buridan's Ass [htmlTitle] => Paradox Resolved: Buridan's Ass [link] => http://steve-patterson.com/wp-content/uploads/2014/10/donkey.jpg [displayLink] => steve-patterson.com [snippet] => Paradox Resolved: Buridan's [htmlSnippet] => Paradox Resolved: Buridan's [mime] => image/jpeg [image] => Array ( [contextLink] => http://steve-patterson.com/paradox-resolved-buridans-ass/ [height] => 434 [width] => 788 [byteSize] => 138112 [thumbnailLink] => https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcSwbhNOJuWaxB11jW9bwmthywOP0-SZictrk0nFOubN_kC3RmFFwuY51lA [thumbnailHeight] => 79 [thumbnailWidth] => 143 ) ) [2] => Array ( [kind] => customsearch#result [title] => Donkey (Shrek) - Wikipedia, the free encyclopedia [htmlTitle] => Donkey (Shrek) - Wikipedia, the free encyclopedia [link] => https://upload.wikimedia.org/wikipedia/en/1/14/Donkey_from_Shrek.jpg [displayLink] => en.wikipedia.org [snippet] => Donkey from Shrek.jpg [htmlSnippet] => Donkey from Shrek.jpg [mime] => image/jpeg [image] => Array ( [contextLink] => https://en.wikipedia.org/wiki/Donkey_(Shrek) [height] => 500 [width] => 288 [byteSize] => 30244 [thumbnailLink] => https://encrypted-tbn2.gstatic.com/images?q=tbn:ANd9GcTWh3l3LYGEt28whI5t1gNNZ0QmZDnEXnFiJjxU4N_nZra_6jKIOGup0cI [thumbnailHeight] => 130 [thumbnailWidth] => 75 ) ) [3] => Array ( [kind] => customsearch#result [title] => Donkey Milk [htmlTitle] => Donkey Milk [link] => http://ep.yimg.com/ay/yhst-134322588545647/donkey-milk-1.gif [displayLink] => www.exoticmeatmarkets.com [snippet] => Donkey Milk [htmlSnippet] => Donkey Milk [mime] => image/gif [fileFormat] => Image Document [image] => Array ( [contextLink] => http://www.exoticmeatmarkets.com/donkeymilk.html [height] => 600 [width] => 485 [byteSize] => 206399 [thumbnailLink] => https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTQTXK38GlIsFBKZ_EoLHNID74zzK_SxOqpTlwz-9Kkd6obb-ouS0-bycw2 [thumbnailHeight] => 135 [thumbnailWidth] => 109 ) ) [4] => Array ( [kind] => customsearch#result [title] => All About DONKEYS! [htmlTitle] => All About DONKEYS! [link] => http://www.lovelongears.com/rabbit.JPG [displayLink] => www.lovelongears.com [snippet] => A small standard donkey, [htmlSnippet] => A small standard donkey, [mime] => image/jpeg [image] => Array ( [contextLink] => http://www.lovelongears.com/about_donkeys2.html [height] => 407 [width] => 432 [byteSize] => 22080 [thumbnailLink] => https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcRPVrm8Yay1R1-7wevhnpHqLgN9gb0cyR0ZHwLvY99ghu4W8VHbvFxzVA [thumbnailHeight] => 119 [thumbnailWidth] => 126 ) ) ) ) [*processed] => Array ( ) )
这看起来更有用,我可以将数组展平以获得一些值,但由于键被重用,我会松开除最后一个结果之外的所有值。具体来说,我希望获得[link]的所有值,其中每个搜索结果中有5个,一个。
我该怎么办?我确信这很容易,但我无法理解。
由于
答案 0 :(得分:0)
好的我对它进行了排序,只是把它变成了一个数组,然后使用了array_walk_recursive:
//Making the returned object into an array
$array = (array) $results;
function array_print($item, $key)
{
if($key=='link'){
echo $item ."<br>";
}
}
array_walk_recursive($array, 'array_print');