我现在正在使用end()
来获取最后一个数组项,但我不想这样做。
我想知道什么时候没有项目,从数组的开头开始。
$current = $_SESSION['current_song'];
$song_array = explode(',', $_SESSION['song_array']);
$nextkey = array_search($current, $song_array) + 1;
$last_song = end($song_array);
if ($nextkey == count($song_array)){
$nextkey == 0;
}
$next = $song_array[$nextkey];
if ($next == $last_song){
$sid = $song_array[0];
} else {
$sid = $next;
}
答案 0 :(得分:0)
while($element = current($song_array)){
// for every item, until we reach the end of the array
// print_r($element) and see what you have...
// when finished, move to next element
next($song_array);
}
// reset pointer to the beginning, if you like
reset($song_array);
答案 1 :(得分:0)
如果您需要多次调用,那么这样的话可能会满足您的需求:
// move pointer to next song
function findnext(&$song_array, $current)
{
do {
if (false === next($song_array)) {
reset($song_array);
}
} while (current($song_array) != $current);
// get next item, rewinding the array if needed
return next($song_array) ?: reset($song_array);
}
$next = findnext($song_array, $current);