<?php
$a=array("red","green","blue","yellow","brown");
print_r(array_slice($a,1,2));
?>
Copy after login
<?php
$a=array("red","green","blue","yellow","brown");
print_r(array_slice($a,-2,1));
?>
Copy after login
<?php
$a=array("red","green","blue","yellow","brown");
print_r(array_slice($a,1,2,true));
?>
Copy after login
<?php
$a=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow","e"=>"brown");
print_r(array_slice($a,1,2));
$a=array("0"=>"red","1"=>"green","2"=>"blue","3"=>"yellow","4"=>"brown");
print_r(array_slice($a,1,2));
?>
Copy after login
<?php
//假定一个结果集二维数组:
$arr = array(array('name'=> 'name1','sex' => 'sex1','job' => 'job1'),
array('name'=> 'name2','sex' => 'sex2','job' => 'job2'),
array('name'=> 'name3','sex' => 'sex3','job' => 'job3'),
array('name'=> 'name4','sex' => 'sex4','job' => 'job4'),
array('name'=> 'name5','sex' => 'sex5','job' => 'job5'),
array('name'=> 'name6','sex' => 'sex6','job' => 'job6'),
array('name'=> 'name7','sex' => 'sex7','job' => 'job7'),
array('name'=> 'name8','sex' => 'sex8','job' => 'job8'),
array('name'=> 'name9','sex' => 'sex9','job' => 'job9'),
array('name'=> 'name10','sex' => 'sex10','job' => 'job10'),
array('name'=> 'name11','sex' => 'sex11','job' => 'job11'),
array('name'=> 'name12','sex' => 'sex12','job' => 'job12'),
);
//计算总记录条数
$num = count($arr);
//规定每页显示的条数
$perpage = 3;
//计算页数
$pages = ceil($num/$perpage);
//echo $num,$perpage,$pagecount;exit;
if(is_numeric($_REQUEST['page']))
{
if($_REQUEST['page']<1){
$page = 1;
}elseif($_REQUEST['page']>$pages)
{
$page = $pages;
}else{
$page = $_REQUEST['page'];
}
}else{
$page = 1;
}
$start = ($page-1)*$perpage;
$newpage = array_slice($arr,$start,$perpage,true);
//print_r($newpage);exit;
?>
<table cellpadding="0" cellspacing="0" border="1">
<tr>
<td>name</td>
<td>sex</td>
<td>job</td>
</tr>
<?php
foreach($newpage as $k => $v)
{
?>
<tr>
<td><?php echo $v['name']; ?></td>
<td><?php echo $v['sex']; ?></td>
<td><?php echo $v['job']; ?></td>
</tr>
<?php
}
?>
</table>
<?php
if($page>1){
echo "<a href='?page=1'>首页</a>";
echo "<a href='?page=".($page-1)."'>上一页</a>";
}
if($page<$pages)
{
echo "<a href='?page=".($page+1)."'>下一页</a>";
echo "<a href='?page=".$pages."'>末页</a>";
}
?>
Copy after login
The above is the detailed content of php function array_slice() that returns a selected part of an array. For more information, please follow other related articles on the PHP Chinese website!
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