我目前正在使用lat.fm api开发应用程序。
我有一个函数可以从last.fm组中提取每周音轨列表。
目的是拉入专辑封面并制作“gridview”模板。
以下代码执行此操作:
public function getWeeklyTrackChartGrid($methodVars) {
// Check for required variables
if ( !empty($methodVars['group']) ) {
$vars = array(
'method' => 'group.getweeklytrackchart',
'api_key' => $this->auth->apiKey
);
$vars = array_merge($vars, $methodVars);
if ( $call = $this->apiGetCall($vars) ) {
$i = 0;
//shuffle($call->weeklytrackchart->track);
$loopN = $call->weeklytrackchart->track;
foreach ($loopN as $track ) {
require 'config.php';
//if(++$i > $userLimit*2) break;
$albumArtS = $tracks['album']['image']['small'] = (string) $track->image[0];
$albumArtM = $tracks['album']['image']['small'] = (string) $track->image[1];
$albumArtL = $tracks['album']['image']['small'] = (string) $track->image[2];
$playCounts = $tracks[$i]['playcount'] = (string) $track->playcount;
?>
<?php if ($playCounts > 1) { ?>
<?php if ($albumArtL) { ?>
<img src="<?php echo $albumArtL; ?>" />
<?php } ?>
<?php } else { ?>
<?php if ($albumArtM) { ?>
<img src="<?php echo $albumArtM; ?>" />
<?php } ?>
<?php } ?>
<?php if ($albumArtwork == "yes") { ?>
<?php if ($albumArt) { ?>
<?php } }?>
<?php $i++;
}
return $tracks;
}
else {
return FALSE;
}
}
else {
// Give a 91 error if incorrect variables are used
$this->handleError(91, 'You must include a group variable in the call for this method');
return FALSE;
}
}
我想在foreach循环中使用php函数shuffle来随机化轨道,而不是按照last.fm给你的顺序将它们拉入。
正如你所看到的,我没有评论上面的随机播放功能,因为当我添加这个时,我收到以下警告:
Warning: shuffle() expects parameter 1 to be array, object given in
关于可能出现什么问题的任何想法?
干杯, 丹
答案 0 :(得分:0)
$loopN = (array) $call->weeklytrackchart->track;
shuffle($loopN);