반복문 안에 반복문
본문
<?php
$tab_h1 = array(
"h1|000|fff|77b900||#",
"h11|cbecf2|000|||#"
);
$tab_h2 = array(
"h1|000|fff|77b900||#",
"h11|cbecf2|000|||#"
);
$tab_h3 = array(
"h1|000|fff|77b900||#",
"h11|cbecf2|000|||#"
);
for ($i=1; $i<4; $i++) { //반목문
$row[] = pfShuffle($tab_h{$i}); //배열을 셔플
echo "<li>";
for ($j=0; $j<2; $j++) { //반목문
$list[$i][] = explode("|", $row[$i][$j]); //배열분리
echo "<div style='float:".$po_float.";width:572px;height:245px;position:relative;'>".$list[$i][0]."</div>";
}//for
echo "</li>";
}//for
?>
위처럼 반복문 3개 안에 반복문 2개 출력하는 코드입니다..........
뭐가 문제일까요 ;;;;;;;;;;
답변 2
소스중에 $tab_h{$i} 이부분이 작동하는건가요??
(저도 궁금해서 정중히 여쭤봅니다)
일단 안된다는 가정하에 탭을 style_arr 라는 배열에 넣고 조금 수정해봤습니다.
2중 반복문 말고 for문을 하나씩 차근차근 하시면 원하던 값이 나올거에요~
배열은 로그 찍을 때 print_r($arr); 인거는 아시지요~
<?php
function pfShuffle(&$items) {
for ($i = count($items) - 1; $i > 0; $i--){
$j = @mt_rand(0, $i);
$tmp = $items[$i];
$items[$i] = $items[$j];
$items[$j] = $tmp;
}
return $items;
}
$tab_h0 = array(
"h1|000|fff|77b900||#",
"h11|cbecf2|000|||#"
);
$tab_h1 = array(
"h1|000|fff|77b900||#",
"h11|cbecf2|000|||#"
);
$tab_h2 = array(
"h1|000|fff|77b900||#",
"h11|cbecf2|000|||#"
);
$style_arr[] = $tab_h0;
$style_arr[] = $tab_h1;
$style_arr[] = $tab_h2;
for ($i=0; $i< count($style_arr); $i++) { //반목문
$row = null;
$row = pfShuffle($style_arr[$i]); //배열을 셔플
echo "<li>";
for ($j=0; $j<2; $j++) { //반목문
$list = null;
$list[$i] = explode("|", $row[$j]); //배열분리
echo "<div style='float:".$po_float.";width:572px;height:245px;position:relative;'>".$list[$i][0]."</div>";
}//for
echo "</li>";
}//for
?>
답변 주신 두분 정말 감사합니다.............많은 도움이 되엇습니다.