Home  >  Article  >  Backend Development  >  php 把一个数组分成有n个元素的二维数组的算法

php 把一个数组分成有n个元素的二维数组的算法

WBOY
WBOYOriginal
2016-06-23 13:48:05780browse

<?php //把一个数组分成几个数组   //$arr 是数组   //$num 是数组的个数   function partition($arr,$num){     //数组的个数     $listcount=count($arr);     //分成$num 个数组每个数组是多少个元素     $parem=floor($listcount/$num);     //分成$num 个数组还余多少个元素     $paremm=$listcount%$num;     $start=0;     for($i=0;$i<$num;$i++){        $end=$i<$paremm?$parem+1:$parem;        $newarray[$i]=array_slice($arr,$start,$end);        $start=$start+$end;     }     return $newarray;   } $arr=array(1,3,4,5,6,7,8,9,2); print_r(partition($arr,3));

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn