开发者

basic question about php multiple FOREACH loops

开发者 https://www.devze.com 2022-12-08 05:55 出处:网络
I have few FOREACH loops in php; 开发者_如何学Python $c1 = 1; $c2 = 1; $c3 = 1; foreach ($someArray as $a){

I have few FOREACH loops in php;

开发者_如何学Python
$c1 = 1;
$c2 = 1;
$c3 = 1;

foreach ($someArray as $a){
    echo $a;
    if (sizeof($someArray != $c1){
        echo " / ";
    }
    $c1++;
}

foreach ($otherArray as $b){
    echo $b;
    if (sizeof($otherArray != $c2){
        echo ", ";
    }
    $c2++;
}

// etc.

This seems somehow stupid, of course =) Is there any way to avoid declaring variables with same values and to use them in many FOREACH loops? Thanks in advance for any help!


It appears you are trying to do what the following code does much better:

$line = implode(' / ', $someArray);
echo $line;

$line = implode(', ', $otherArray);
echo $line;
0

精彩评论

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

关注公众号