Get the App
SLTechnology News&Howtos  ›  Development  › 

How to intercept the last few bits of an array by php

Shulou Source: shulou.com Published: 2022-06-03 08:37:26 10月04日 Update

This article is about how php intercepts the last few bits of an array. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Php intercepts the last few elements of the array: 1, use the "array_slice ($array,$start)" statement, the parameter "$start" should be negative; 2, use the "array_splice ($array,$start)" statement, the parameter "$start" should be negative.

Operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

Php intercepts the last few elements of the array

1. Use the array_slice () function

The array_slice () function is a function provided by PHP to intercept an array and extract a fragment from the array. The syntax is as follows:

Array array_slice (array $arr, int $start [, int $length = NULL [, bool $preserve_keys = false]])

Parameter description:

Arr represents the array to be intercepted.

Start indicates where to start the intercept (subscript):

If start is positive, it is intercepted from the back of the go.

If the start is negative, it is intercepted from back to front, starting at the distance from the end of the arr-start. For example,-2 means starting with the penultimate element of the array.

Length is an optional parameter that indicates the intercept length:

If length is positive, it indicates the number of elements intercepted

If length is negative, the intercepted fragment will terminate at the position of length at the end of the distance array

If omitted, it starts at the start position and goes all the way to the end of the array.

Preserve_keys is an optional parameter, which specifies whether to keep the original key name. The default is false, that is, it is not retained. If set to true, the original key name will be retained.

Example: intercept the last few elements of an array

To intercept the last few elements of the array, simply set the parameter start to a negative value

Output result

2. Use the array_splice () function

When the array_splice () function deletes some elements of an array, it combines the deleted elements into a new array and then returns the new array; so the array_splice () function can be used to intercept the array fragments.

As with the array_slice () function, you can intercept the last few elements of the array simply by setting the second parameter, start, to a negative value.

Output result

Thank you for reading! This is the end of this article on "how php intercepts the last few bits of the array". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Tags: Arrays elements functions parameters positions negative values fragments content just more ends positive numbers articles results statements negative numbers output good practical subscript Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple OPPO Reno Linux MariaDB Huawei