2017-08-31 78 views
0
{#186 ▼ 
    +"results": Collection {#203 ▼ 
    #items: array:15 [▼ 
     0 => {#218 ▼ 
     +"wrapperType": "track" 
     +"kind": "song" 
     +"artistId": 32940 
     +"collectionId": 159292399 
     +"trackId": 159293848 
     +"artistName": "Michael Jackson" 
     +"collectionName": "The Essential Michael Jackson" 
     +"trackName": "Billie Jean" 
     +"collectionCensoredName": "The Essential Michael Jackson" 
     +"trackCensoredName": "Billie Jean (Single Version)" 
     +"artistViewUrl": "https://itunes.apple.com/us/artist/michael-jackson/id32940?uo=4" 
     +"collectionViewUrl": "https://itunes.apple.com/us/album/billie-jean-single-version/id159292399?i=159293848&uo=4" 
     +"trackViewUrl": "https://itunes.apple.com/us/album/billie-jean-single-version/id159292399?i=159293848&uo=4" 
     +"previewUrl": "https://audio-ssl.itunes.apple.com/apple-assets-us-std-000001/AudioPreview122/v4/c6/50/11/c6501132-e865-3711-175a-ddb79114e42f/mzaf_3806132797788612279.plus.aac ▶" 
     +"artworkUrl30": "http://is2.mzstatic.com/image/thumb/Music127/v4/8a/65/be/8a65bef2-f23d-e43d-9124-f5e4293513f7/source/30x30bb.jpg" 
     +"artworkUrl60": "http://is2.mzstatic.com/image/thumb/Music127/v4/8a/65/be/8a65bef2-f23d-e43d-9124-f5e4293513f7/source/60x60bb.jpg" 
     +"artworkUrl100": "http://is2.mzstatic.com/image/thumb/Music127/v4/8a/65/be/8a65bef2-f23d-e43d-9124-f5e4293513f7/source/100x100bb.jpg" 
     +"collectionPrice": 16.99 
     +"trackPrice": 1.29 
     +"trackNumber": 16 
     +"trackTimeMillis": 294601 
     +"country": "USA" 
     +"currency": "USD" 
     +"primaryGenreName": "Pop" 
     +"isStreamable": true 
     } 
     1 => {#217 ▶} 
     2 => {#216 ▶} 
     3 => {#215 ▶} 

回答

1

你應該能夠做到這一點與一個班輪:

$total = $response->results->sum('trackTimeMillis'); 
+0

我想爲其他15個數組獲得trackTimerMilis以及 –

+0

'sum'方法將爲集合中的每個項目添加「trackTimerMillis」值。看一看'Illuminate \ Support \ Collection'的'sum'方法 - https://laravel.com/docs/5.4/collections#method-sum – Desh901

+0

哦,你想總結和檢索它們中的每一個? – Desh901

2

這是一個簡單的可迭代對象,沒有什麼特別的或Laravel相關的。

你可以這樣做:

$sum = 0; 

foreach ($response['results']->items as $item) { 
    $sum += $item->trackTimeMillis; 
} 

var_dump($sum); 
+0

不能使用類型爲stdClass的對象作爲數組 –

+0

對不起,我看錯了採集。用更新後的代碼重試。 – GiamPy