简体   繁体   English

我如何从多维数组中获取数据

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

I am having some trouble with an object that is return from the google php api client. 我从Google PHP API客户端返回的对象遇到了一些麻烦。 I have tried posting using that tag but had no help. 我尝试使用该标签发布,但没有帮助。 I have since converted the object to an array, but still can't figure out how to get all the data I need. 从那以后,我将对象转换为数组,但仍然不知道如何获取所需的所有数据。 Basically I am doing a search using google api, and it returns an object with the results which is as follows (using print_r): 基本上我正在使用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 ( ) 
 )

I was having trouble retrieving the values I wanted because some items are missing values in this, the first one is [contextDataType:protected] => This causes all sorts of problems, and I am not sure why the object is returned like this. 我在检索所需的值时遇到了麻烦,因为其中某些项目缺少值,第一个是[contextDataType:protected] =>这会引起各种问题,而且我不确定为什么要这样返回对象。

So I converted to an array: $array = (array) $results; 所以我转换成一个数组:$ array =(array)$ results; This gives: 这给出:

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 ( ) ) 

This looks much more usable, and I can flatten the array to get some values, but since the keys are reused, I then loose all but the last result. 这看起来更有用,并且我可以展平数组以获取一些值,但是由于键被重用,因此除了最后一个结果外,我将失去所有值。 Specifically I want to get all the values of [link] of which there are 5, one in each search result. 具体来说,我想获取[link]的所有值,每个搜索结果中有5个。

How can I do this please? 我该怎么做? I am sure this is easy but I can't figure it out. 我确信这很容易,但我无法弄清楚。

Thanks 谢谢

OK I sorted it, just made it into an array and then used array_walk_recursive: 好的,我对它进行了排序,将其放入一个数组中,然后使用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