for 문이 어떻게 되나요~~

for 문이 어떻게 되나요~~

QA

for 문이 어떻게 되나요~~

본문

다음과 같은 것을 for 문으로 표현하려는데 도통 에러만 나고 되질 않네요~~ 어디가 잘못인가요?


$latest_out_01 = $cf_bs02['1'];
$latest_skin_01 = $cf_bs02['2'];
$latest_board_id_01 = $cf_bs02['3'];
$latest_group_id_01 = $cf_bs02['4'];
$latest_line_number_01 = $cf_bs02['5'];
$latest_text_lenght_01 = $cf_bs02['6'];
$latest_out_02 = $cf_bs02['7'];
$latest_skin_02 = $cf_bs02['8'];
$latest_board_id_02 = $cf_bs02['9'];
$latest_group_id_02 = $cf_bs02['10'];
$latest_line_number_02 = $cf_bs02['11'];
$latest_text_lenght_02 = $cf_bs02['12'];
$latest_out_03 = $cf_bs02['13'];
$latest_skin_03 = $cf_bs02['14'];
$latest_board_id_03 = $cf_bs02['15'];
$latest_group_id_03 = $cf_bs02['16'];
$latest_line_number_03 = $cf_bs02['17'];
$latest_text_lenght_03 = $cf_bs02['18'];
$latest_out_04 = $cf_bs02['19'];
$latest_skin_04 = $cf_bs02['20'];
$latest_board_id_04 = $cf_bs02['21'];
$latest_group_id_04 = $cf_bs02['22'];
$latest_line_number_04 = $cf_bs02['23'];
$latest_text_lenght_04 = $cf_bs02['24'];
$latest_out_05 = $cf_bs02['25'];
$latest_skin_05 = $cf_bs02['26'];
$latest_board_id_05 = $cf_bs02['27'];
$latest_group_id_05 = $cf_bs02['28'];
$latest_line_number_05 = $cf_bs02['29'];
$latest_text_lenght_05 = $cf_bs02['30'];
$latest_out_06 = $cf_bs02['31'];
$latest_skin_06 = $cf_bs02['32'];
$latest_board_id_06 = $cf_bs02['33'];
$latest_group_id_06 = $cf_bs02['34'];
$latest_line_number_06 = $cf_bs02['35'];
$latest_text_lenght_06 = $cf_bs02['36'];
$latest_out_07 = $cf_bs02['37'];
$latest_skin_07 = $cf_bs02['38'];
$latest_board_id_07 = $cf_bs02['39'];
$latest_group_id_07 = $cf_bs02['40'];
$latest_line_number_07 = $cf_bs02['41'];
$latest_text_lenght_07 = $cf_bs02['42'];
 
위의 내용을 아래와 같이 했습니다
for($j=1;$j<=42;$j+=6)
${"latest_out_0".sprintf('%d', $j+5/6)} = $cf_bs02[$j];
${"latest_skin_0".sprintf('%d', $j+5/6)} = $cf_bs02[$j+1];
${"latest_board_id_0".sprintf('%d', $j+5/6)} = $cf_bs02[$j+2];
${"latest_group_id_0".sprintf('%d', $j+5/6)} = $cf_bs02[$j+3];
${"latest_line_number_0".sprintf('%d', $j+5/6)} = $cf_bs02[$j+4];
${"latest_text_lenght_0".sprintf('%d', $j+5/6)} = $cf_bs02[$j+5];

이 질문에 댓글 쓰기 :

답변 3


for ($i = 1; $i <= 7; $i++) {
    $latest_out[$i] = $cf_bs02[($i - 1) * 6 + 1];
    $latest_skin[$i] = $cf_bs02[($i - 1) * 6 + 2];
    $latest_board_id[$i] = $cf_bs02[($i - 1) * 6 + 3];
    $latest_group_id[$i] = $cf_bs02[($i - 1) * 6 + 4];
    $latest_line_number[$i] = $cf_bs02[($i - 1) * 6 + 5];
    $latest_text_length[$i] = $cf_bs02[($i - 1) * 6 + 6];
}

답변감사합니다~~ 덕분에 잘 해결하였습니다~~

그런데 혹시 위에 제가 한것은 무엇이 잘못되었는지 가르침 주실수 있는지요~~
제가 이리저리 홈피만들면서 재미삼아 공부중이거든요~~ 서비스는 컨텐츠가 없어서 불가하구요~~

채택 감사 합니다.

sprintf('%d', $j+5/6) 표현식이 올바르지 않습니다.
$j 값에 5/6을 더한 후 정수로 변환하려고 하지만, 이 표현식은 의도한 대로 작동하지 않습니다.
대신, 올바른 인덱스를 계산하기 위해 (int)(($j + 5) / 6)를 사용할 수 있습니다.

들레아빠님 추가 답변 고맙습니다~~ 혼자 공부하자니 맨날 초보 탈출이 안됩니다

초보라고 하시면 이미 초보를 벗어 나셨습니다. 공부는 영원히 다할 수 없습니다.


for($j = 1; $j <= 42; $j += 6) {
    ${"latest_out_0".(int)(($j + 5) / 6)} = $cf_bs02[$j];
    ${"latest_skin_0".(int)(($j + 5) / 6)} = $cf_bs02[$j + 1];
    ${"latest_board_id_0".(int)(($j + 5) / 6)} = $cf_bs02[$j + 2];
    ${"latest_group_id_0".(int)(($j + 5) / 6)} = $cf_bs02[$j + 3];
    ${"latest_line_number_0".(int)(($j + 5) / 6)} = $cf_bs02[$j + 4];
    ${"latest_text_length_0".(int)(($j + 5) / 6)} = $cf_bs02[$j + 5];
}

감사합니다~~ 위로까지 ~퇴직 백수의 길이 아주 즐겁습니다

처음에 알려주신 것은 이상없이 잘 되는데 아래 제게 올린것 수정해주신 것은 되기는 되는데 아래와 같은 오류가 한줄 뜨네요~~

Warning: Undefined array key 42 in D:\Apache24\htdocs\theme\dw_bootstrap\include\out_option.php on line 138

 

위의 138라인이 

${"latest_text_length_0".(int)(($j + 5) / 6)} = $cf_bs02[$j + 5]; 

거든요
답변을 작성하시기 전에 로그인 해주세요.
전체 61,021
QA 내용 검색

회원로그인

(주)에스아이알소프트 / 대표:홍석명 / (06211) 서울특별시 강남구 역삼동 707-34 한신인터밸리24 서관 1404호 / E-Mail: admin@sir.kr
사업자등록번호: 217-81-36347 / 통신판매업신고번호:2014-서울강남-02098호 / 개인정보보호책임자:김민섭(minsup@sir.kr)
© SIRSOFT