foreach 에 배열 3개..

foreach 에 배열 3개..

QA

foreach 에 배열 3개..

본문

총 풀어야 할 배열이 3개인데요

어떻게 풀어야 할까요?

 

wr_question / wr_answer / wr_example 이렇게 3개의 배열인데요

 

1888875639_1636562633.5573.png

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

PHP 5.4 버전부터 short_open_tag 설정과 상관없이 <?= 사용이 가능합니다.

- https://www.php.net/manual/en/ini.core.php#ini.short-open-tag
Note: This directive does not affect the shorthand <?=, which is always available.

* https://php-legacy-docs.zend.com/manual/php5/en/migration54.new-features
<?= is now always available, regardless of the short_open_tag php.ini option.


<? } 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++; } } ?>
답변을 작성하시기 전에 로그인 해주세요.
전체 253
QA 내용 검색

회원로그인

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