Home >Backend Development >PHP Problem >How to intercept the first few elements of an array in php

How to intercept the first few elements of an array in php

王林
王林Original
2020-08-06 13:12:558797browse

How to intercept the first few elements of an array in php: You can use the array_splice() function to achieve this. The array_splice() function removes selected elements from an array and returns an array of removed elements. The specific usage method is: [array_slice($arr,0,4);].

How to intercept the first few elements of an array in php

array_splice() function removes the selected element from an array and can replace it with a new element. The function will also return an array of removed elements.

(Recommended tutorial: php graphic tutorial)

Syntax:

array_splice(array,start,length,array)

Parameters:

  • array Required. Specifies an array.​

  • #start Required. numerical value. Specifies the starting position of deleted elements.

  • length Optional. numerical value. Specifies the number of elements to be removed, which is also the length of the returned array.

(Video tutorial recommendation: php video tutorial)

Example:

Get the first 4 digits of the array

array_slice($arr,0,4);

Get the last 4 digits of the array

array_slice($arr,-4,4);

or

array_slice($arr,-4);

The above is the detailed content of How to intercept the first few elements of an array in php. For more information, please follow other related articles on the PHP Chinese website!

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