foreach 에 배열 3개..
본문
총 풀어야 할 배열이 3개인데요
어떻게 풀어야 할까요?
wr_question / wr_answer / wr_example 이렇게 3개의 배열인데요
wr_question 은 포이치 돌려서 풀었는데 나머지 두개는 동시에 어덯게 풀어야 하나요?
<? } else if($w == 'u') {
$wr_question = explode(',', $write['wr_question']);
$index = 0;
foreach($wr_question as $row) {
?>
<tr>
<th>문제</th>
<td>
<div><input type="text" name="wr_question[]" value="<?php echo $row ?>" required class="frm_input full_input required"></div>
<div class="margin">정답 <input type="text" name="wr_answer[]" value="" required class="frm_input full_input required answer"> 번</div>
</td>
<th class="center">보기</th>
<td>
<div class="flex">1. <input type="text" name="wr_example[]" value="" required class="frm_input required" style="flex:1; margin-left:1rem"></div>
<div class="flex">2. <input type="text" name="wr_example[]" value="" required class="frm_input required" style="flex:1; margin-left:1rem"></div>
<div class="flex">3. <input type="text" name="wr_example[]" value="" required class="frm_input required" style="flex:1; margin-left:1rem"></div>
<div class="flex">4. <input type="text" name="wr_example[]" value="" required class="frm_input required" style="flex:1; margin-left:1rem"></div>
</td>
</tr>
<? $index++; } } ?>
답변 3
<?php
} else if ( $w=='u' ) {
$wr_question = explode(',', $write['wr_question']);
$wr_answer = explode(',', $write['wr_answer']);
$wr_example = array_chunk(explode(',', $write['wr_example']), 4);
foreach ($wr_question as $k=>$question) { // 편의상 $row가 아닌, $question 변수명 지정
$answer = $wr_answer[$k]; // 정답
$examples = $wr_example[$k]; // 보기 배열
?>
<tr>
<th>문제</th>
<td>
<div><input type="text" name="wr_question[]" value="<?=$question?>" required class="frm_input full_input required"></div>
<div class="margin">정답 <input type="text" name="wr_answer[]" value="<?=$answer?>" required class="frm_input full_input required answer"> 번</div>
</td>
<th class="center">보기</th>
<td>
<?php
foreach ( $examples as $k2=>$example ) {
$no = $k2+1;
?><div class="flex"><?=$no?>. <input type="text" name="wr_example[]" value="<?=$example?>" required class="frm_input required" style="flex:1; margin-left:1rem"></div><?php
}
?>
</td>
</tr>
<?php
}
}
<? } else if($w == 'u') {
$wr_question = explode(',', $write['wr_question']);
$wr_answer = explode(',', $write['wr_answer']);
$wr_example = explode(',', $write['wr_example']);
foreach($wr_example as $key => $val) {
$arr = explode(' ',$val);
$example_arr[$arr[0]] = $arr[1];
}
$index = 0;
foreach($wr_question as $key => $val) {
?>
<tr>
<th>문제</th>
<td>
<div><input type="text" name="wr_question[]" value="<?php echo $val ?>" required class="frm_input full_input required"></div>
<div class="margin">정답 <input type="text" name="wr_answer[]" value="<?php echo $wr_answer[$key] ?>" required class="frm_input full_input required answer"> 번</div>
</td>
<th class="center">보기</th>
<td>
<?php for($i=1; $i<=sizeof($example_arr[$val]); $i++) { ?>
<div class="flex"><?php $i ?>. <input type="text" name="wr_example[]" value="<?php echo $example_arr[$val][$i]?>" required class="frm_input required" style="flex:1; margin-left:1rem"></div>
<?php } ?>
</td>
</tr>
<? $index++; } } ?>
여분 컬럼 쓰지 마시고
테이블 분리해서 저장하시는 걸 추천합니다.
답변을 작성하시기 전에 로그인 해주세요.