A brief discussion of PHP source code twenty-one: About the array_slice function

不言
Release: 2023-04-01 22:56:02
Original
1695 people have browsed it

This article mainly introduces about the PHP source code 21: array_slice function, which has a certain reference value. Now I share it with you. Friends in need can refer to it.

Brief Talk PHP source code twenty-one: array_slice function

array_slice
(PHP 4, PHP 5)

array_slice — Take out a section from the array
Description
array array_slice (array array , int offset [, int length [, bool preserve_keys]] )

array_slice() Returns a sequence in the array array specified by the offset and length parameters.

If offset is non-negative, the sequence will start at this offset in array. If offset is negative, the sequence will start this far from the end in the array.

If length is given and positive, there will be this many cells in the sequence. If length is given and negative, the sequence will terminate this far from the end of the array. If omitted, the sequence will start at offset and go to the end of array.
This section explains that the implementation in the program is very simple, as follows:

while (pos < offset + length && ...) { ....... }
Copy after login

Note that array_slice() will reset the keys of the array by default. As of PHP 5.0.2, this behavior can be changed by setting preserve_keys to TRUE.

The reset of the keys of the array mentioned here refers to the reset of the numeric keys in the array.
The PHP code is shown below:

 2, 1 => 1, 's' => 's', '0' => 'ddd'); var_dump(array_slice($arr, 0)); die(); /* 此段程序会输出:array(4) { [0]=> int(2) [1]=> int(1) ["s"]=> string(1) "s" [2]=> string(3) "ddd" } */
Copy after login

In the source code we can see:

Copy after login

The above is the entire content of this article. I hope it will be helpful to everyone's study. More related Please pay attention to the PHP Chinese website for content!

Related recommendations:

A brief discussion on PHP source code twenty: About array_flip, array_reverse functions

A brief discussion on PHP source code ten Nine: About array_file, range function

The above is the detailed content of A brief discussion of PHP source code twenty-one: About the array_slice function. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!