2014-10-20 3 views
0

내 코드에서 for 루프를 가지고 : 나는 $d의 정확한 번호를 모르는 경우for 루프의 마지막 반복을 어떻게 알 수 있습니까?

$d = count($v2['field_titles']); 

for($i=0; $i < $d; $i++) { 
    $current = 3 + $i; 
    echo 'current is' . $current; 
} 

어떻게 마지막 반복을 알 수 있습니까?

$d = count($v2['field_titles']); 

for($i=0; $i < $d; $i++) { 
    $current = 3 + $i; 

    if(this is the last iteration) { 
    echo 'current is ' . $current; 
    // add sth special to the output 
    } 
    else { 
    echo 'current is ' . $current; 
    } 
} 
+0

은 $ i가 $ D해야한다 -이 간단한 경우 마지막 반복에서 1, ... – njzk2

+1

, 당신은 단지'에 STH 특수를 추가하려면 루프가 완료된 후에 출력이 출력됩니다. –

+0

@Vohuman 중복이 아님. 내용이나 제목 만 읽었습니까? – JasonMArcher

답변

2
if($i==$d-1){ 
    //last iteration :) 
} 
0

내가 while을 선호 개인적으로 for에 :

내가 좋아하는 일을하고 싶습니다. 나는 이것을 할 것이다 :

$array = array('234','1232','234'); //sample array 
$i = count($array); 
while($i--){ 
    if($i==0) echo "this is the last iteration"; 
    echo $array[$i]."<br>"; 
} 

나는이 유형의 루프가 조금 더 빠르지 만 개인적으로 검증되지는 않았 음을 읽었다. 확실히 읽기/쓰기가 더 쉽습니다. 귀하의 경우

이가 번역 것 :

$d = count($v2['field_titles']); 

while($d--) { 
    $current = 3 + $d; 

    if($d==0) { 
    echo 'current is ' . $current; 
    // add sth special to the output 
    } 
    else { 
    echo 'current is ' . $current; 
    } 
} 
+1

노력해 주셔서 감사 합니다만 어쨌든'for'을 사용하겠습니다. –

관련 문제