Does anyone know why this php iteration won't work
Does anyone know why this doesn't work
function my_current($array) {
return current($array);
}
$array = array(1,3,5,7,13);
while($i = my_current($array)) {
$content .= $i.',';
next($array);
}
yet this does
$array = array(1,3,5,7,13);
while($i = current($array)) {
$content .= $i.',';
next($array);
}
or how to make the top one work? It's a little question but it开发者_StackOverflow would be a big help! Thanks Richard
The array is copied, which means that the current pointer is lost. Pass it as a reference.
function my_current(&$array) {
Or better yet, use implode()
.
By default a copy of the array is being made.
Try this:
function my_current(&$array) {
return current($array);
}
I guess it's because when you call a function with an array parameter, the array is copied over. Try using references.
function my_current(&$array) {
return current($array);
}
Notice the &
.
精彩评论