<?php $uids = array(1,2,3,4,5,6,7); $uids_num = count($uids); $k = $j = 0; $uids_group = array(); for ($i = 0; $i < $uids_num; $i++) { if (($i+1) % 2 == 0) { $uids_group[$k][$j] = $uids[$i]; $k += 1; $j = 0; } else { $uids_group[$k][$j] = $uids[$i]; $j++; } } print_r($uids_group);exit;
第二种方法:
$uids = array(1,2,3,4,5,6,7); $limit = 2; $uids_group = array(); $total = count($uids);//总个数 $pages = ceil($total/$limit);//可以分成几组 for ($i = 1; $i <= $pages; $i++) { $uids_group[] = array_slice($uids, ($i-1) * $limit, $limit); } print_r($uids_group);exit;
第三种方法:
$uids = array(1,2,3,4,5,6,7); $uids_group = array_chunk($uids,3); print_r($uids_group);exit;