仅处理第一个外部for-each循环

时间:2011-05-05 06:20:02

标签: php

我在for-each循环中有for-each循环

类似于:

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

现在我只需要处理第一个外部循环,而不是每隔一次处理。

我该怎么做?

3 个答案:

答案 0 :(得分:2)

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

或者您可以这样做,只需:

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

答案 1 :(得分:0)

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

答案 2 :(得分:0)

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