Home >php教程 >PHP源码 >大量数据,数组切割分页

大量数据,数组切割分页

PHP中文网
PHP中文网Original
2016-05-25 17:14:14973browse

跳至

可以将一大批量数据(前提是一维数组)分割处理成按需要的块。
传入参数为要分割的一维数组,和分割单元的大小。
返回一个切割好了的二维数组。第一维为各个块,第二维为具体数据。




/**
 * designed by kalcaddle
 * 数组分页函数.	 
 * 可以将一个数组按照指定大小切割成新的数组
 * 
 * 参数:原始一维数组,切割大小
 * 返回值:新的二维数组,纵列为切割页数.
 * 
 * 使用实例:	
 * for($i = 0; $i < 175; $i++) 
 *		{$send[$i] = $i;} 
 * $arr = arr_page($send, 4654);
 * $cc = count($arr);
 * for($i = 0; $i < $cc; $i++) {
 *		$tt = count($arr[$i]);
 *		for($j = 0; $j < $tt; $j++) {
 *		echo "
arr[$i][$j]=" . $arr[$i][$j];
 *		} 
 * }
 */

public function arr_page($arr,$line)  //数组分页,将一个一维数组按指定大小切割组装成一个二维数组
	{	
		$num=count($arr); 
		$page = floor($num / $line) + ($num % $line == 0?0:1); //得到分页数目 
		$i=0;$j=0;
		while($j<$page) 
		{
			if($j==$page-1) 
			{
				$last=$num%$line;
				$start=$num-$last;
				for($i=0; $i<$last; $i++) 
				{
					$arr_page[$j][$i]=$arr[$start+$i];
				}
				break;
			}
			for($i=0; $i<$line; $i++) 
			{	
				$arr_page[$j][$i]=$arr[$j*$line+$i];
			}
			$j++;
		}
		Return $arr_page;
	}

                   

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