簡體   English   中英

我如何從多維數組中獲取數據

[英]How can I get data from this multidimensional array

我從Google PHP API客戶端返回的對象遇到了一些麻煩。 我嘗試使用該標簽發布,但沒有幫助。 從那以后,我將對象轉換為數組,但仍然不知道如何獲取所需的所有數據。 基本上我正在使用google 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個。

我該怎么做? 我確信這很容易,但我無法弄清楚。

謝謝

好的,我對它進行了排序,將其放入一個數組中,然后使用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');

暫無
暫無

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

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