开发者

processing only for first outer for-each loop

开发者 https://www.devze.com 2023-03-02 18:55 出处:网络
I have a for-each 开发者_如何学Cloop, within for-each loop similar to: foreach ($arr1 as $v1) { foreach ($v1 as $v2) {

I have a for-each 开发者_如何学Cloop, within for-each loop

similar to:

 foreach ($arr1 as $v1) {
     foreach ($v1 as $v2) {
          [ processing goes here ]
     }
 }

Now I need to processing only for first outer loop, not for every other time.

How can I do it?


foreach ($arr1 as $v1) {
     foreach ($v1 as $v2) {
          [ processing goes here ]
     }
break;
}

Or you might do, simply:

foreach ($arr1[0] as $v2) {
    [ processing goes here ]
}


$clone = $arr1;   // clone for future use
foreach (array_shift($clone) as $v2) {   // loop for first element of clone of $arr1, if starting index is known, $arr1[0] can be used instead of array_shift($clone)
          [ processing goes here ]
     }


$firstTime = true;
foreach ($arr1 as $v1) {
    if($firstTime) {
        $firstTime = false;
        foreach ($v1 as $v2) {
            [ processing goes here ]
        }
    }
}
0

精彩评论

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

关注公众号