Detailed description of array_slice function usage examples in PHP

Source: Internet
Author: User
This article mainly introduces the array_slice function usage in PHP, analyzes the specific meaning and usage skills of the array_slice function in the form of instances, and uses the paging method as an example to provide specific application instances, some references

This article mainly introduces the array_slice function usage in PHP, analyzes the specific meaning and usage skills of the array_slice function in the form of instances, and uses the paging method as an example to provide specific application instances, some references

This article describes in detail the usage of the array_slice function and some common array_slice instance programs for your reference. The specific analysis is as follows:

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

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 the result.

Parameters:

Array required. specifies the input array.

Offset is required. It is a numerical value that specifies the start position of the retrieved element. If it is a positive number, it is obtained from the beginning to the end. If it is a negative value, it is taken from the end to the absolute value of the offset.

Length (optional) value. specify the length of the returned array. If it is a negative number, select the element with the absolute value from the back to the front. If this value is not set, all elements are returned.

Optional. Value Options: true-retain key false-default-Reset key. When the value is 0, assign the value to a new variable and return the variable.

The Code is as follows:

The Code is as follows:

<? Php
$ 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) ""}}

The paging method implemented by the array_slice function is very easy to use. The following is an example:

The Code is as follows:

<? Php
// 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;
?>



















<? PhpForeach ($ newpage as $ k => $ v){?> <? Php}?>
Name Sex Job
<? Php echo $ v ['name'];?> <? Php echo $ v ['sex'];?> <? Php echo $ v ['job'];?>

<? Php
If ($ page> 1 ){
Echo "Homepage ";
Echo "Previous Page ";
}
If ($ page <$ pages)
{
Echo "next page ";
Echo "last page ";
}
?>

I hope this article will help you with PHP programming.

,

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.