/**
* array_size自定义分页
* @param $data
* @param int $currentPage
* @param int $perPage
* @return array
*/
function get_page($data, $currentPage = 1, $perPage = 10)
{
$totalItems = count($data);
$currentPage = max(1, $currentPage);
$offset = ($currentPage - 1) * $perPage;
$totalPages = ceil($totalItems / $perPage);
$pagedData = array_slice($data, $offset, $perPage);
return [
'totalItems' => $totalItems,
'currentPage' => $currentPage,
'perPage' => $perPage,
'totalPages' => $totalPages,
'data' => $pagedData
];
}
/**
* 自定义分页利用array_slice函数对数组进行分页
* @param $info 需要分页的数组
* @param $page 当前页数
* @param int $pageSize 每页条数
* @return mixed
*/
function doPage($info, $page = 1, $pageSize = 10)
{
$info = array_values((array)$info);
$start = ($page - 1) * $pageSize;
$data['total'] = count($info);
$data['current_page'] = $page;
$data['per_page'] = $pageSize;
$data['last_page'] = ceil($data['total'] / $pageSize);
$data['data'] = array_slice($info, $start, $pageSize);
return $data;
}
网友评论