테스트 사이트 - 개발 중인 베타 버전입니다

배열을 for문으로 출력하는데 잘 안됩니다..ㅠㅠ 채택완료

발렌슈테인 4년 전 조회 2,465

text.txt파일에는

가,나,다,라,마,바,사,아,자,차,카,타,파,하,1,2,3,4,5,6,7,8,9,10,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z
들어가있으며 지정된 숫자만큼 순차적으로 출력할려고 합니다
1. 가,나,다,라,마
2. 바,사,아,자,차
3. 카,타,파,하,1
4. 2.3.4.5.6
단순하게 print_r로 배열확인하니 잘 나와서 for문으로 할려는데 배열을 못가져옵니다 ㅠㅠ

<?php
    $text = @file("test.txt") or $result = "파일을 읽을 수 없습니다.";    
    if ($text  != null) {
        for($i = 0; $i < count($text); $i++){
            $cnt = 5;
            $arr = explode(",", $text[$i]);
            $arr1 = array_chunk($arr, $cnt);
            for($j = 0;$j < $cnt;$j++){
                $result .= $arr1[$j].",";
            }
        }
    }
?>

<p><?php echo $result; ?></p>


초보자에게 가르침좀 내려주십시오

댓글을 작성하려면 로그인이 필요합니다.

답변 5개

채택된 답변
+20 포인트
유판
4년 전

현제 이렇게 해서 배열로 원하는 값만큼 출력되는데요
이걸 for문으로 바꿔서 사용하고 싶어서 질문을 올린겁니다

 

일단 이렇게 말씀하시니 답글이 많지만 수정한 코드를 적습니다.

 

</p>

<p><?php

    $text = @file("test.txt") or $result = "파일을 읽을 수 없습니다.";    

    if ($text  != null) {

        for($i = 0; $i < count($text); $i++){  // 현재 한줄이시므로 이런식의 카운터는 필요 없습니다.

            $cnt = 5;

            $arr = explode(",", $text[$i]);

            $arr1 = array_chunk($arr, $cnt);

            for($j = 0;$j < $cnt;$j++){

                $result .= $arr1[$j].",";

            }

        }

    }

?></p>

<p><p><?php echo $result; ?></p></p>

<p>

 

를 기준으로 아래와 같이 수정했습니다.

 

</p>

<p><?php</p>

<p>    $cnt = 5; // 분할갯수

    $text = @file("test.txt") or $result = "파일을 읽을 수 없습니다.";    

    if ($text  != null) {</p>

<p>        $arr = explode(",", $text[0]);  // 한줄을 , 으로 분할</p>

<p>        for($i = 0; $i < count($arr); $i++){</p>

<p>            if($i != 0 && ($i+1) % $cnt == 0){ // $i 가 0이 아니고, $cnt의 나머지가 0일 때</p>

<p>               // $i+1인 이유는 시작이 0부터 하기 때문 1부터 시작하면 1부터 하셔도 되며 코드가 달라짐</p>

<p>               $result .= "
".(( ($i+1) / $cnt) + 1).". "; // 다음줄 넘기기</p>

<p>            }else if($i == 0){</p>

<p>               $result .= "1. "; // 1 넣기</p>

<p>            }else{</p>

<p>               $result .= ","; // 콤마 추가</p>

<p>            }</p>

<p>            $result .= $arr[$i]; // 숫자 추가</p>

<p>        }

    }

?></p>

<p><p><?php echo $result; ?></p></p>

<p>

로그인 후 평가할 수 있습니다

답변에 대한 댓글 1개

발렌슈테인
4년 전
아..감사합니다 제가 만든거랑 비교해보니 뭐가 잘못인지 알꺼같습니다

댓글을 작성하려면 로그인이 필요합니다.

B
4년 전

</p>

<p><?php</p>

<p>// 파일의 내용을 읽어, 좌우 여백 제거 후 콤마 기준으로로 분리, 5개씩 나눠서 할당</p>

<p>$temp = array_chunk(explode(',', trim(file_get_contents('./test.txt'))), 5);

$result = ''; // 결과값 초기화

foreach ($temp as $key=>$values) $result.= ($key+1).'. '.implode(',', $values).'
'; // 번호 추가해 결합

echo $result;</p>

<p>/* 실행 결과</p>

<p><span style="color: rgb(0, 0, 0); font-family: "Malgun Gothic"; font-size: medium; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">1. 가,나,다,라,마</span><br style="color: rgb(0, 0, 0); font-family: "Malgun Gothic"; font-size: medium; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;" />
<span style="color: rgb(0, 0, 0); font-family: "Malgun Gothic"; font-size: medium; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">2. 바,사,아,자,차</span><br style="color: rgb(0, 0, 0); font-family: "Malgun Gothic"; font-size: medium; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;" />
<span style="color: rgb(0, 0, 0); font-family: "Malgun Gothic"; font-size: medium; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">3. 카,타,파,하,1</span><br style="color: rgb(0, 0, 0); font-family: "Malgun Gothic"; font-size: medium; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;" />
<span style="color: rgb(0, 0, 0); font-family: "Malgun Gothic"; font-size: medium; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">4. 2,3,4,5,6</span><br style="color: rgb(0, 0, 0); font-family: "Malgun Gothic"; font-size: medium; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;" />
<span style="color: rgb(0, 0, 0); font-family: "Malgun Gothic"; font-size: medium; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">5. 7,8,9,10,a</span><br style="color: rgb(0, 0, 0); font-family: "Malgun Gothic"; font-size: medium; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;" />
<span style="color: rgb(0, 0, 0); font-family: "Malgun Gothic"; font-size: medium; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">6. b,c,d,e,f</span><br style="color: rgb(0, 0, 0); font-family: "Malgun Gothic"; font-size: medium; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;" />
<span style="color: rgb(0, 0, 0); font-family: "Malgun Gothic"; font-size: medium; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">7. g,h,i,j,k</span><br style="color: rgb(0, 0, 0); font-family: "Malgun Gothic"; font-size: medium; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;" />
<span style="color: rgb(0, 0, 0); font-family: "Malgun Gothic"; font-size: medium; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">8. l,m,n,o,p</span><br style="color: rgb(0, 0, 0); font-family: "Malgun Gothic"; font-size: medium; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;" />
<span style="color: rgb(0, 0, 0); font-family: "Malgun Gothic"; font-size: medium; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">9. q,r,s,t,u</span><br style="color: rgb(0, 0, 0); font-family: "Malgun Gothic"; font-size: medium; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;" />
<span style="color: rgb(0, 0, 0); font-family: "Malgun Gothic"; font-size: medium; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">10. v,w,x,y,z</span></p>

<p>*/</p>

<p>

로그인 후 평가할 수 있습니다

답변에 대한 댓글 2개

발렌슈테인
4년 전
감사합니다 BiHon님 덕분에 foreach를 어떻게 활용해야하는지 하나더 배웠습니다
유판
4년 전
foreach 배워두시면 쓰실 데가 많습니다 :)
Key이름을 다 가져 올 수도 있구욥 :)

댓글을 작성하려면 로그인이 필요합니다.

<?php
    $text = @file("test.txt") or $result = "파일을 읽을 수 없습니다.";    
    if ($text  != null) {

            $cnt = 1;

            $size=5;
        for($i = 0; $i < count($text); $i++){
            if( $i % $size == 0) echo $cnt++,'. ';

            echo $text[ $i];

            if( $i % $size == 0 && $i) echo '<br />';

            else echo ',';
        }
    }
?>

<p><?php echo $result; ?></p>

로그인 후 평가할 수 있습니다

댓글을 작성하려면 로그인이 필요합니다.

https://www.php.net/manual/en/function.array-chunk.php

 

<?php
$input_array = array('a', 'b', 'c', 'd', 'e');
print_r(array_chunk($input_array, 2));

Array
(
    [0] => Array
        (
            [0] => a
            [1] => b
        )

    [1] => Array
        (
            [0] => c
            [1] => d
        )

    [2] => Array
        (
            [0] => e
        )

)
로그인 후 평가할 수 있습니다

답변에 대한 댓글 2개

발렌슈테인
4년 전
<?php
$text = @file("test.txt") or $result = "파일을 읽을 수 없습니다.";
$cnt = 5;
$arr = explode(',',$text [0]);
$arr1 = array_chunk($arr, $cnt);
print_r($arr1);


현제 이렇게 해서 배열로 원하는 값만큼 출력되는데요

이걸 for문으로 바꿔서 사용하고 싶어서 질문을 올린겁니다

txt파일안에는

가,나,다,라,마,바,사,아,자,차,카,타,파,하,1,2,3,4,5,6,7,8,9,10,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z

이렇게 한줄로 들어가있습니다

주소남겨주신걸로 참고해서 해보겠습니다 감사합니다
릴보이즈
4년 전
음.. 일단 \n 이나 br 태그로 줄 바꿈 있어야되구요
1부터 n번까지 앞에 번호가 있어야 될거 같아요

댓글을 작성하려면 로그인이 필요합니다.

저걸 그냥 txt말고 array에 넣은것은요? 

 

로그인 후 평가할 수 있습니다

답변에 대한 댓글 1개

발렌슈테인
4년 전
아..배열 확인은
<?php
$text = @file("test.txt") or $result = "파일을 읽을 수 없습니다.";
$cnt = 5;
$arr = explode(',',$text [0]);
$arr1 = array_chunk($arr, $cnt);
print_r($arr1);
?>
이렇게 해서 확인했는데 배열로 잘 받아옵니다..

댓글을 작성하려면 로그인이 필요합니다.

답변을 작성하려면 로그인이 필요합니다.

로그인