Home > Backend Development > PHP Tutorial > Detailed explanation of array_slice function usage examples in PHP, array_slice examples_PHP tutorial

Detailed explanation of array_slice function usage examples in PHP, array_slice examples_PHP tutorial

WBOY
Release: 2016-07-13 10:13:15
Original
1971 people have browsed it

Detailed explanation of array_slice function usage examples in PHP, array_slice examples

This article introduces the detailed usage of the array_slice function and some commonly used array_slice example programs, sharing it with everyone for your reference. The specific analysis is as follows:

The array_slice() function takes out a segment of value from the array based on conditions and returns.

Note: If the array has string keys, the returned array will retain the key names, see Example 4.

Syntax: array_slice(array,offset,length,preserve)

The function takes out a value from the array based on conditions and returns.

Parameters:

array required, specifies the input array.

offset is required, a numerical value, which specifies the starting position of the element to be taken out. If it is a positive number, it will be taken from the front to the back. If it is a negative value, the offset absolute value will be taken from the back to the front.

length Optional, numerical value, specifies the length of the returned array. If it is a negative number, the absolute number of elements of the value will be selected from back to front. If the value is not set, all elements will be returned.

preserve is optional, possible values: true - reserved key false - default - reset key, when it is 0, assign the value inside to a new variable, and finally return this variable.

The code is as follows:

Copy code The code is as follows:
$arr = array(0,1,2,3,4);
var_dump(array_slice($arr,0,2));
     
echo "
";
$arr2 = array('a'=>array('a','a','a'),'b'=>array('b','b','b'));
var_dump(array_slice($arr2,0,1));
?>

The returned results are as follows:
array(2) { [0]=> int(0) [1]=> int(1) }
array(1) { ["a"]=> array(3) { [0]=> string(1) "a" [1]=> string(1) "a" [2]=> string(1) "a" } }

The paging method implemented by the array_slice function is very easy to use, share it as follows:

Copy code The code is as follows:
//假定一个结果集二维数组:
   $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;
?>

 
 
 
 
 
 foreach($newpage as $k => $v)
{
?>
 
 
 
 
 
}
?>
name sex job

if($page>1){
 echo "首页";
 echo "上一页";
}
if($page<$pages)
{
 echo "下一页";
 echo "末页";
}
?>

希望本文所述对大家的PHP程序设计有所帮助。

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/917021.htmlTechArticlePHP中array_slice函数用法实例详解,array_slice实例 本文详细介绍了array_slice函数的详细用法以及一些常用的array_slice实例程序,分享给大家供大家...
Related labels:
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