PHP Array_slice Function Usage example detailed _php

Source: Internet
Author: User
In this paper, the detailed usage of the Array_slice function and some common Array_slice instance programs are introduced in detail, which are shared for everyone's reference. The specific analysis is as follows:

The Array_slice () function takes a value out of the array and returns it as a condition.

Note: If the array has a string key, the returned array will retain the key name, see Example 4.

Syntax: Array_slice (Array,offset,length,preserve)

The function takes a value out of the array and returns it as a condition.

Parameters:

Array required, which specifies the input arrays.

Offset required, numeric, specifies the starting position of the element to be removed, if it is a positive number, the previous start to take, if a negative value, from the back forward to the absolute value.

The length is optional, the value is specified, and if it is a negative number, the element that has the absolute value of the values is selected, and if the value is not set, all elements are returned.

Preserve optional, possible value: true – Reserved key false– default – Resets the key, when it is 0, assigns the value inside to a new variable, and finally returns 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 "
";
$arr 2 = Array (' A ' =>array (' A ', ' a ', ' a '), ' B ' =>array (' B ', ' B ', ' B '));
Var_dump (Array_slice ($arr 2,0,1));
?>

The results returned 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) "A"}}

The paging method implemented by the Array_slice function is very useful and is shared as follows:

The code is as follows:

<?php
Assume a two-dimensional array of result sets:
$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 ' = ' 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 '),
);

Calculate total number of records
$num = count ($arr);
Specify the number of bars to display per page
$perpage = 3;
Count 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;
?>



















<?php foreach ($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 "Home";
echo "";
echo "Last";
}
?>

I hope this article is helpful to everyone's 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.