Introduction to Array_slice function usage in PHP _php tutorial

Source: Internet
Author: User
This article specifically introduces the detailed usage of the Array_slice function and some commonly used array_slice instance programs, which can be seen by the students who need to know.

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 retains the key name. (see Example 4)

Grammar
Array_slice (Array,offset,length,preserve)

The function takes a value out of a condition in the array and returns
Parameters
Array required. Specifies the input array.
Offset required. Numerical. Specifies the starting position of the element to be removed. If it is a positive number, start with the previous one, and if it is negative, take the offset from the back to the absolute value.
Length is optional. Numerical. Specifies the length of the returned array. If it is a negative number, select the element that has an absolute value from backward forward. If the value is not set, all elements are returned.
Preserve is optional. Possible values: true – Reserved key false– default – Reset key

When it is 0, the value of the inside is assigned to a new variable, and finally the variable is returned.
Today, idle to turn the manual, originally this thing 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 "
";

$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:

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

I found the Array_slice function. Very useful, share:

The code is as follows Copy Code

Assume a two-dimensional array of result sets:

$arr = Array (' name ' = ' name1 ', ' sex ' = ' sex1 ', ' job ' = ' job1 '),
Array (' name ' = ' name2 ', ' sex ' =& Gt ' 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 '),
Arr Ay (' 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;
?>







foreach ($newpage as $k = $v)
{
?>




}
?>

Name Sex Job

if ($page >1) {
echo "Home";
echo "Prev";
}
if ($page < $pages)
{
echo "Next page";
echo "Last";
}
?>


http://www.bkjia.com/PHPjc/445308.html www.bkjia.com true http://www.bkjia.com/PHPjc/445308.html techarticle This article specifically introduces the detailed usage of the Array_slice function and some commonly used array_slice instance programs, which can be seen by the students who need to know. Array_slice () function in the array root ...

  • 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.