将一维数组平均分成几个数组

浏览:1595 最后更新:2016-03-28 14:34 分类:函数
/*
$arrF:数组
$user_count:分成几组
$group_num:每组多少个
*/
function array_group($arrF,$user_count,$group_num){
for($i=0;$i<$user_count;$i++){
if($i == $user_count-1){
$arrT[] = array_slice($arrF, $i * $group_num );
}else{
$arrT[] = array_slice($arrF, $i * $group_num ,$group_num);
}
}
return $arrT;
}

$arrF = array('001','002','003','004','005','006','007','008','009','0010');
$user_count = "3"; //分成几组
$group_num = floor(count($arrF)/$user_count); //每组多少个
$arr = array_group($arrF,$user_count,$group_num);
var_dump($arr);die;


输出:

array(3) {
[0] => array(3) {
[0] => string(3) "001"
[1] => string(3) "002"
[2] => string(3) "003"
}
[1] => array(3) {
[0] => string(3) "004"
[1] => string(3) "005"
[2] => string(3) "006"
}
[2] => array(4) {
[0] => string(3) "007"
[1] => string(3) "008"
[2] => string(3) "009"
[3] => string(4) "0010"
}
}
评论( 相关
后面还有条评论,点击查看>>