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

WBOY
Release: 2016-06-23 13:48:05
Original
839 people have browsed it

<?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));
Copy after login

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template