2017-08-06 26 views
0

のは、私はYouTubeのAPI V3を使用して、簡単なユーチューブ検索を行うと、私はこのような応答を取得するとしましょう:YouTubeのAPI JSONレスポンス(PHP)から1本のランダムビデオをピッキング

{ 
"kind": "youtube#searchListResponse", 
"etag": "\"m2yskBQFythfE4irbTIeOgYYfBU/1MwZbx-vX21eNjUJjkUlNHKCIhI\"", 
"nextPageToken": "CAIQAA", 
"regionCode": "US", 
"pageInfo": { 
    "totalResults": 412, 
    "resultsPerPage": 2 
}, 
"items": [ 
    { 
    "kind": "youtube#searchResult", 
    "etag": "\"m2yskBQFythfE4irbTIeOgYYfBU/q2KHbIuwnAhM64HgrqhBqc5xvTI\"", 
    "id": { 
    "kind": "youtube#video", 
    "videoId": "gpZvuZEiINA" 
    }, 
    "snippet": { 
    "publishedAt": "2014-03-30T11:46:50.000Z", 
    "channelId": "UCCaE0Bj6NI-y8_yL1FpcJUw", 
    "title": "Depression-90` instrumental (download link)", 
    "description": "E-Mail: [email protected]\nNew old school instrumental with download link ,Enjoy the beat, I hope you like it!\nPlease comment, rate and subscribe if you like this beat\nI appreciate YOUR support! Peace and respect", 
    "thumbnails": { 
    "default": { 
     "url": "https://i.ytimg.com/vi/gpZvuZEiINA/default.jpg", 
     "width": 120, 
     "height": 90 
    }, 
    "medium": { 
     "url": "https://i.ytimg.com/vi/gpZvuZEiINA/mqdefault.jpg", 
     "width": 320, 
     "height": 180 
    }, 
    "high": { 
     "url": "https://i.ytimg.com/vi/gpZvuZEiINA/hqdefault.jpg", 
     "width": 480, 
     "height": 360 
    } 
    }, 
    "channelTitle": "Ekii020 90` OLDSCHOOL - BOOMBOOMBAP INSTRUMENTALS", 
    "liveBroadcastContent": "none" 
    } 
    }, 
    { 
    "kind": "youtube#searchResult", 
    "etag": "\"m2yskBQFythfE4irbTIeOgYYfBU/HXRyGhFaD4Cn0wvF5XPMJKX5tNQ\"", 
    "id": { 
    "kind": "youtube#video", 
    "videoId": "G7ThqpcuPTI" 
    }, 
    "snippet": { 
    "publishedAt": "2017-08-05T23:30:24.000Z", 
    "channelId": "UCsFmkkSVNgvihycqqtjSXgA", 
    "title": "Homeless (Dark Sad Piano Hip Hop Rap Instrumental Beat)", 
    "description": "http://rightbeatradio.com/product/homeless/\n\nI wrote a song about a humble man I met yesterday. We spoke for a while. He just wanted to talk. He was homeless.\n\nI cut some pieces from a few jazz recordings and put them behind a simple piano riff.\n\nThis music has a lot of feeling. Perfectly describes how I felt after our conversation.\n\nTake care.\n\n89 bpm\n\nrightbeatradio.com\ntwitter.com/rightbeatradio", 
    "thumbnails": { 
    "default": { 
     "url": "https://i.ytimg.com/vi/G7ThqpcuPTI/default.jpg", 
     "width": 120, 
     "height": 90 
    }, 
    "medium": { 
     "url": "https://i.ytimg.com/vi/G7ThqpcuPTI/mqdefault.jpg", 
     "width": 320, 
     "height": 180 
    }, 
    "high": { 
     "url": "https://i.ytimg.com/vi/G7ThqpcuPTI/hqdefault.jpg", 
     "width": 480, 
     "height": 360 
    } 
    }, 
    "channelTitle": "Right Beat Radio", 
    "liveBroadcastContent": "none" 
    } 
    } 
] 
} 

私は生成したいと思い、例えば10の結果が返されますが、レスポンスからランダムに1つ(1つ)ランダム[item]を選択します。結果をランダム化するにはどうしたらいいですか?

私はshuffle()array_rand()のような機能を見てきましたが、これらは私が必要とする機能ではないようです。

私もこのように書きスニペットを見てきました

$array = json_decode($JSON, true); 
$random_entry = array_rand($array['items'], 1); 
$json_data = json_encode($random_entry); 

私はそれを試してみたが、それは「nextPageToken」または「種類」のような文字列のみを返すなど

私は考えなど、1 全体[item]を選ぶと、そのようなchannelId、タイトル、説明、サムネイルとしてその関連[snippet]データのすべてをつかむのが好き

誰もが正しい方向に私を指すことができますか?

答えて

1

この

$array = json_decode($JSON, true); 
$items = $array['items']; 
$random_key = array_rand($items, 1); 
$json_data = json_encode($items[$random_key]); 

array_rand()の戻り配列のキーではなく、配列の値をお試しください。

+0

これは速くて素晴らしいものでした。ありがとうございます。 – anthonyCam

関連する問題