我正在使用一个包装器,它可以非常简单地获取搜索词并将其转换为数组。
我在解析此数组时遇到问题。如何创建foreach()循环以从此数组中获取值?
这是抓取搜索词并转换为数组的简单函数。 print_r()只显示返回的内容。:
require_once (get_template_directory() . '/Classes/itunes.php');
function itunesSearch() {
$search_term = $_GET['s'];
$results = iTunes::search($search_term, array(
'country' => 'US'
))->results;
print_r($results);
}
例如,让我们说我搜索阿黛尔。这是要返回的数组:
数组([0] => stdClass对象([wrapperType] => track [kind] => song [artistId] => 262836961 [collectionId] => 1051394208 [trackId] => 1051394215 [ artistName] => Adele [collectionName] => 25 [trackName] => Hello [collectionCensoredName] => 25 [trackCensoredName] => Hello [artistViewUrl] => https://itunes.apple.com/us/artist/adele/id262836961?uo=4 [collectionViewUrl] =&gt ; https://itunes.apple.com/us/album/hello/id1051394208?i=1051394215&uo=4 [trackViewUrl] => https://itunes.apple.com/us/album/hello/id1051394208?i=1051394215&uo=4 [previewUrl] => http://a1912.phobos.apple.com/us/r1000/170/Music6/v4/68/34/f1/6834f1f8-8fdb-4247-492a-c0caea580082/mzaf_3920281300599106672.plus.aac.p.m4a [artworkUrl30] => http://is5.mzstatic.com/image/thumb/Music6/v4/8c/91/5d/8c915d9b-d9e4-f735-1b91-81ca1b6e6312/source/30x30bb.jpg [artworkUrl60] => http://is5.mzstatic.com/image/thumb/Music6/v4/8c/91/5d/8c915d9b-d9e4-f735-1b91-81ca1b6e6312/source/60x60bb.jpg [artworkUrl100] => http://is5.mzstatic.com/image/thumb/Music6/v4/8c/91/5d/8c915d9b-d9e4-f735-1b91-81ca1b6e6312/source/100x100bb.jpg [collectionPrice] => 10.99 [trackPrice] => 1.29 [releaseDate] => 2015-11-20T08:00:00Z [collectionExplicitness] => notExplicit [trackExplicitness] => notExplicit [discCount] => 1 [discNumber] => 1 [trackCount] => 11 [trackNumber] => 1 [trackTimeMillis] => 295502 [country] => USA [currency] = > USD [primaryGenreName] => Pop [radioStationUrl] => https://itunes.apple.com/station/idra.1051394215 [isStreamable] =>)
[1] => stdClass对象([wrapperType] => track [kind] => song [artistId] => 262836961 [collectionId] => 420075073 [trackId] => 420075084 [artistName] => Adele [collectionName] =&gt ; 21 [trackName] =>在深层滚动[collectionCensoredName] => 21 [trackCensoredName] =>在深层滚动[artistViewUrl] => https://itunes.apple.com/us/artist/adele/id262836961?uo=4 [collectionViewUrl] => {{3} } [trackViewUrl] => https://itunes.apple.com/us/album/rolling-in-the-deep/id420075073?i=420075084&uo=4 [previewUrl] => https://itunes.apple.com/us/album/rolling-in-the-deep/id420075073?i=420075084&uo=4 [artworkUrl30] => http://a818.phobos.apple.com/us/r1000/115/Music/3d/fd/74/mzm.dqadcdcf.aac.p.m4a [artworkUrl60] => http://is5.mzstatic.com/image/thumb/Music/v4/cf/7e/47/cf7e47a8-bb18-9156-43d0-7591d0e0855e/source/30x30bb.jpg [artworkUrl100] =&gt ; http://is5.mzstatic.com/image/thumb/Music/v4/cf/7e/47/cf7e47a8-bb18-9156-43d0-7591d0e0855e/source/60x60bb.jpg [collectionPrice] => 10.99 [trackPrice] => 1.29 [releaseDate] => 2011-02-22T08:00:00Z [collectionExplicitness] => notExplicit [trackExplicitness] => notExplicit [ discCount] => 1 [discNumber] => 1 [trackCount] => 12 [trackNumber] => 1 [trackTimeMillis] => 228293 [country] =>美国[货币] => USD [primaryGenreName ] => Pop [radioStationUrl] => http://is5.mzstatic.com/image/thumb/Music/v4/cf/7e/47/cf7e47a8-bb18-9156-43d0-7591d0e0855e/source/100x100bb.jpg [isStreamable] => 1)......
这是我尝试过的,以及其中的几种变体:
$results_thing = $results['stdClass']['Object'];
foreach ($results_thing as $result) {
echo $result['wrapperType'];
我收到错误:
Warning: Invalid argument supplied for foreach()
答案 0 :(得分:1)
假设 $ results 包含上面提供的数组 。然后获取'wrapperType'的代码......
foreach($results as $result) {
$wrapper_type = $result->wrapperType;
}