开发者

Does anyone know why this php iteration won't work

开发者 https://www.devze.com 2023-01-29 18:37 出处:网络
Does anyone know why this doesn\'t work function my_current($array) { return current($array); } $array = array(1,3,5,7,13);

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 &.

0

精彩评论

暂无评论...
验证码 换一张
取 消