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;
?>
| Name |
Sex |
Job |
<? PhpForeach ($ newpage as $ k => $ v){?>
| <? Php echo $ v ['name'];?> |
<? Php echo $ v ['sex'];?> |
<? Php echo $ v ['job'];?> |
<? Php}?>
<? 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.
,