带有逗号分隔符的foreach循环导致错误


foreach loop with comma separator causing errors

我正在我的网站上实现jplayer,需要在php中生成播放列表。我几乎已经制定了逗号分隔,但我用来获取歌曲url和歌曲标题的调用导致了php错误,我不太确定我做错了什么。我使用的社交引擎是zend的扩展。我通常能够使用<?php echo $this->string()->truncate($song->getTitle(), 50) ?><?php $current_url = explode('?', $song->getFilePath()); echo $current_url[0]; ?>来生成我的mp3标题和url,而在for each循环中没有问题。我的代码在下面,有人能给我指正确的方向吗?

<?php
  $count = 0;
  foreach( $songs as $song => $item): if( !empty($song) ): ?>
  <?php if ( $count ) { print ", "; } $count++; ?>
{
    title:"<?php echo $this->string()->truncate($song->getTitle(), 50) ?>",
    mp3:"<?php $current_url = explode('?', $song->getFilePath());
echo $current_url[0]; ?>"
}
<?php endif; endforeach; ?>

edit:我看到的错误是"在…player.tpl中的非对象上调用成员函数getTitle()"

您在循环中使用$song,应该使用$item$song只是数组键,因此没有名为getTitle的方法,而$item是您的实际对象。