여분필드 배열정렬 해결해주세요.ㅠ

여분필드 배열정렬 해결해주세요.ㅠ

QA

여분필드 배열정렬 해결해주세요.ㅠ

본문

현재 아래와 같이 여분필드를 이용해서 view화면에 출력을 하고 있습니다.

wr_2기준으로 가나다순 순서로 정렬을 하고 싶은데 방법이 없나요?ㅠㅠ


  <?
   $wr_body_1 = explode("|",substr($view[wr_1], 1));
   $wr_body_2 = explode("|",substr($view[wr_2], 1));
   $wr_body_3 = explode("|",substr($view[wr_3], 1));
   $wr_body_4 = explode("|",substr($view[wr_4], 1));
   $wr_body_5 = explode("|",substr($view[wr_5], 1));
   $wr_body_6 = explode("|",substr($view[wr_6], 1));
   $wr_body_7 = explode("|",substr($view[wr_7], 1));
   $wr_body_8 = explode("|",substr($view[wr_8], 1));
   for ($i = 0;  $i < count($wr_body_1); $i++) {
    echo
     "
     <tr height='35'>
      <td width='100' align='center'>$wr_body_1[$i]</td>
      <td style='padding-left:10px;'>$wr_body_2[$i]</td>
      <td width='100' align='center'>$wr_body_3[$i]</td>
      <td width='80' style='padding-left:10px;'>$wr_body_4[$i]원</td>
      <td width='80' align='center'>$wr_body_5[$i] 개</td>
      <td width='80' align='center'>$wr_body_6[$i] 개</td>
      <td width='80' align='center'>$wr_body_7[$i] 개</td>
      <td width='80' align='center'>$wr_body_8[$i] 개</td>
     </tr>
     ";
   }
  ?>​
 

 

이 질문에 댓글 쓰기 :

답변 2

배열의 정렬은 내장함수 sort 로 할 수 있습니다.

bool sort    ( array &$array   [, int $sort_flags  ] )

이 함수는 배열을 정렬한다. 이 함수가 수행되면 원소들은 작은 값에서 큰 값으로 정렬될것이다.

 

아래와 같이 해보세요 



for($i=1;  $i<9; $i++){
    ${'wr_body_'.$i} = explode('|',substr($view['wr_'.$i], 1));
    if($i > 1) sort(${'wr_body_'.$i});
}


for ($i = 0;  $i < count($wr_body_1); $i++) {
    echo
     ";
     <tr height='35'>
      <td width='100' align='center'>$wr_body_1[$i]</td>
      <td style='padding-left:10px;'>$wr_body_2[$i]</td>
      <td width='100' align='center'>$wr_body_3[$i]</td>
      <td width='80' style='padding-left:10px;'>$wr_body_4[$i]원</td>
      <td width='80' align='center'>$wr_body_5[$i] 개</td>
      <td width='80' align='center'>$wr_body_6[$i] 개</td>
      <td width='80' align='center'>$wr_body_7[$i] 개</td>
      <td width='80' align='center'>$wr_body_8[$i] 개</td>
     </tr>
     ";​
}
​

 

안되네요ㅠㅠ

인터넷에 찾아보다 아래와 같이 짜집기 하니깐 되는데 쓸때없는 소스가 있나요?

 


   $wr_body_1 = explode("|",substr($view[wr_1], 1));
   }
   $wr_body_3 = explode("|",substr($view[wr_3], 1));
   $wr_body_4 = explode("|",substr($view[wr_4], 1));
   $wr_body_5 = explode("|",substr($view[wr_5], 1));
   $wr_body_6 = explode("|",substr($view[wr_6], 1));
   $wr_body_7 = explode("|",substr($view[wr_7], 1));
   $wr_body_8 = explode("|",substr($view[wr_8], 1));
   array_multisort($wr_body_1, SORT_ASC, $wr_body_2, SORT_ASC, $wr_body_3, SORT_ASC, $wr_body_4, SORT_ASC, $wr_body_5, SORT_ASC, $wr_body_6, SORT_ASC, $wr_body_7, SORT_ASC, $wr_body_8, SORT_ASC);
   for ($i = 0;  $i < count($wr_body_1); $i++) {
    echo
     "
     <tr height='35'>
      <td width='100' align='center'>$wr_body_1[$i]</td>
      <td style='padding-left:10px;'>$wr_body_2[$i]</td>
      <td width='100' align='center'>$wr_body_6[$i]</td>
      <td width='80' style='padding-left:10px;'>US$ $wr_body_3[$i]</td>
      <td width='80' align='center'>$wr_body_4[$i] 개</td>
     </tr>
     ";
   }​
 
답변을 작성하시기 전에 로그인 해주세요.
전체 125,873 | RSS
QA 내용 검색

회원로그인

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