Array_slice function usage in PHP

Source: Internet
Author: User

This article specifically introduces the usage of array_slice functions and some common array_slice instance programs.

The array_slice () function extracts a value from the Array Based on the Conditions and returns it.

Note: If the array has a string key, the returned array retains the key name. (See example 4)

Syntax
Array_slice (array, offset, length, preserve)

The function extracts a value from the Array Based on the Conditions and returns
Parameters
Array is required. Specifies the input array.
Offset is required. Value. Specifies the start position of the retrieved element. If it is a positive number, it is taken from the beginning to the end. If it is a negative value, it is taken from the back to the absolute value of the offset.
Length is optional. Value. Specifies the length of the returned array. If it is a negative number, select the element of the absolute value from the back to the front. If this value is not set, all elements are returned.
Preserve is optional. Possible value: true-retain key false-default-Reset key

 

When the value is 0, assign the value to a new variable and return the variable.
Today, I flipped through the manual. It turns out that there is 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 ));
?>

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) ""}}

I found the array_slice function. Very easy to use. Share the following:

The Code is as follows: Copy code

// Assume a two-dimensional array of the result set:

$ Arr = 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' => 'job1 '),
Array ('name' => 'name5', 'sex' => 'sex5 ', 'job' => 'job5 '),
Array ('name' => 'name6', 'sex' => 'sex6', 'job' => 'job1 '),
Array ('name' => 'name7', 'sex' => 'sex7', 'job' => 'job2 '),
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' => 'job2 '),
);

 
// Calculate the total number of records
$ Num = count ($ arr );
// Specify the number of entries displayed on each page
$ Perpage = 3;
// Calculate the page number
$ 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 "Homepage ";
Echo "Previous Page ";
}
If ($ page <$ pages)
{
Echo "next page ";
Echo "last page ";
}
?>

 


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.