0
我目前正在使用lat.fm api来开发一个应用程序。last.fm api - 随机/随机foreach循环
我有一个函数可以运行在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;
}
}
我想用PHP函数洗牌的foreach循环随机化的轨道,而不是拉不动,在顺序last.fm给他们你。
正如你可以看到我还没有发表意见的随机播放功能上面,因为当我加入这个我得到以下警告:
Warning: shuffle() expects parameter 1 to be array, object given in
任何想法上可能是什么问题呢?
干杯, 丹
是更换$ loopN变种? – danyo
是的,shuffle()中的参数必须是数组,如果您将一个对象$ call-> weeklytrackchart-> track放入其中,它将无法工作。只需使用(array)将对象转换为数组。 –
我已经试过这个,但它似乎没有输出任何东西? – danyo