繁体   English   中英

这实际上有效,但还有另一种循环方式吗?

[英]This actually works but is there another way to loop?

 // Loop $key

$key = count($_SESSION['imageURL']);

for ($i = 1; $i <= $key; $i++) {

    echo $_SESSION['imageURL'][$i];

    echo $_SESSION['clubURL'][$i];

}
foreach ($_SESSION['imageURL'] as $k=>$image)
{
   echo $image;
   echo $_SESSION['clubURL'][$k];
}

还有其他几种方法:

foreach

foreach ($_SESSION['imageURL'] as $key => $image) {
    echo $image;
    echo $_SESSION['clubURL'][$key];
}

while

while (list ($key, $image) = each ($_SESSION['imageURL']) {
    echo $image;
    echo $_SESSION['clubURL'][$key];
}

do..while

if (count($_SESSION['imageURL']) {
    do {
        echo current($_SESSION['imageURL']);
        echo $_SESSION['clubURL'][key($_SESSION['imageURL'])];
    } while (next($_SESSION['clubURL']));
}

就个人而言,我更喜欢你的for循环技术。

您仍然可以使用foreach 应该看起来像这样:

foreach ($_SESSION['imageURL'] as $image) {
    echo $image;
}

为了仅 go两个arrays 保持值(请参阅 OP 对问题的评论):

// $shortest holds the length of the *shortest* array, i.e., iteration
// only goes as far as both arrays have indexes.
$shortest = min(count($_SESSION['imageURL']), count($_SESSION['clubURL']));
for ($i = 0; $i < $shortest; $i++) {
    echo $_SESSION['imageURL'][$i];
    echo $_SESSION['clubURL'][$i];
}

笔记
这仅在两个 arrays 是“平行的”时才有效,即$_SESSION['imageURL']的第 n 个值匹配$_SESSION['clubURL']的第 n 个值直到任何(或两者)结束arrays 的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM