Array_slice function usage example in PHP, array_slice instance _ PHP Tutorial

Source: Internet
Author: User
For example, array_slice function usage in PHP. The array_slice function usage examples in PHP are described in detail. the array_slice instance describes the detailed usage of the array_slice function and some common array_slice instance programs, array_slice instance

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.

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

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.