Skip to content
On this page

배열을 12개씩 내부에 4개씩 묶어서 출력할경우

배열이 1부터 12개까지 출력하는데 1배열에 4개씩 묶어서 출력하려면 어떻게 하는지 찾아보니까..

이렇게 하면된다.

php
$list = [];
// 리뷰글 가져오기
$sql = " select * from `{$g5['g5_shop_item_use_table']}` where is_confirm = '1' order by is_time desc limit 0,12";
$result = sql_query($sql);
for ($i=0; $rows = sql_fetch_array($result); $i++) {
$list[$i]['subject'] = conv_subject($rows['is_subject'],12,"");
$list[$i]['content'] = conv_content($rows['is_content'],2);
$list[$i]['score'] = get_star($rows['is_score']);
$list[$i]['writer'] = mb_substr(get_text($rows['is_name']), 0, 1, 'utf-8') . "***" . mb_substr($rows['is_name'], -1, 0, 'utf-8');
$list[$i]['datetime'] = date('Y-m-d',strtotime($rows['is_time']));
}

$output = array_chunk($list,4,true);

여기서 중요한게 array_chunk 함수를 이용해서 4개씩 묶어서 출력한다.