开发者

This actually works but is there another way to loop?

开发者 https://www.devze.com 2023-03-11 01:08 出处:网络
// Loop $key $key = count($_SESSION[\'imageURL\']); for ($i = 1; $i <= $key; $i++) { echo $_SESSION[\'image开发者_开发问答URL\'][$i];
 // Loop $key

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

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

    echo $_SESSION['image开发者_开发问答URL'][$i];

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

}


There are several other ways:

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']));
}

Personally, I prefer your technique with the for loop.


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


You can still use foreach. Should look somehow like this:

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


In order to only go as far as both arrays hold values (see OP's comment on question):

// $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];
}

Note
This only works if the two arrays are "parallel", i.e., the n'th value of $_SESSION['imageURL'] matches the n'th value of $_SESSION['clubURL'] until the end of any (or both) of the arrays.

0

精彩评论

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