Home > Backend Development > PHP Tutorial > Introduction to the usage of array_slice function in PHP_PHP tutorial

Introduction to the usage of array_slice function in PHP_PHP tutorial

WBOY
Release: 2016-07-20 11:03:04
Original
1306 people have browsed it

This article specifically introduces the detailed usage of the array_slice function and some commonly used array_slice example programs. Students who need to know more can take a look. ​

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

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

Grammar
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 required. numerical value. Specifies the starting position of the element to be retrieved. If it is a positive number, it is taken from the front to the back. If it is a negative value, the offset absolute value is taken from the back to the front.
length is optional. numerical value. Specifies the length of the returned array. If it is a negative number, select the absolute number of elements of the value from back to front. If the value is not set, all elements are returned.
preserve is optional. Possible values: true – retain key false – default – reset key

When it is 0, assign the value inside to a new variable, and finally return this variable.
I flipped through the manual while I was idle today, and it turns out that this thing has a ready-made function: array_slice.

The code is as follows Copy code
 代码如下 复制代码

    $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));
?>

$arr = array(0,1,2,3,4);

var_dump(array_slice($arr,0,2));
 代码如下 复制代码
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" } }
    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:
The code is as follows Copy code
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" } }

I found the array_slice function. Very useful, share it:

The code is as follows array('name'=> 'name8','sex' => 'sex8','job' => 'job8'), array('name'=> 'name9','sex' => 'sex9','job' => 'job9'), array('name'=> 'name10','sex' => 'sex10','job' => 'job10'),
 代码如下 复制代码

//假定一个结果集二维数组:

   $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
Copy code



//Assume a result set two-dimensional array:

$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'=> 'name11','sex' => 'sex11','job' => 'job11'), array('name'=> 'name12','sex' => 'sex12','job' => 'job12'), ); //Calculate the total number of records $num = count($arr); //Set the number of items displayed on each page $perpage = 3; //Calculate the number of pages $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 "Home"; echo "Previous page"; } if($page<$pages)<🎜> {<🎜> echo "next page";<🎜> echo "last page";<🎜> }<🎜> ?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/445308.htmlTechArticleThe article specifically introduces the detailed usage of the array_slice function and some commonly used array_slice example programs, which you need to know Students can take a look. The array_slice() function is rooted in the array...
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