Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How php deletes the last two elements of an array

2025-03-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article is about how php deletes the last two elements 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 can use the array_splice () function to delete the elements of the last two items of the array, this function is used to delete a specified number of elements from a specified location, only need to set the second parameter of the array_splice () function to "- 2", syntax "array_splice ($arr,-2)".

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

Php deletes the last two elements of the array

In php, you can use the array_splice () function to delete the elements of the last two items of the array.

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

The syntax of array_splice () is as follows:

Array array_splice (array & $arr, int $start [, int $length = 0 [, mixed $replacement]])

Parameter description:

Arr represents an array.

Start indicates where to start the deletion (subscript):

If start is positive, it is deleted from after going.

If the start is negative, it is deleted from back to front, starting 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 number of elements deleted:

If length is positive, it means deleting length elements

If length is negative, all elements from start to the reciprocal length at the end of the array will be deleted

If omitted, all elements from start to the end of the array are deleted.

Replacement is an optional parameter that represents the value to replace. If replacement has more than one value, it needs to be set to an array, and if there is only one value, it can not be set to an array.

Therefore, to delete the last two elements of the array, you can set the value of the start parameter to-2:

The output is as follows:

Thank you for reading! This is the end of this article on "how to delete the last two elements of the array by php". 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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report