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 to find the value of the last element of an array by php

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces the knowledge of "how to find the value of the last element of the array by php". Many people will encounter this dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Five methods: 1, use "array_pop ($arr)" statement, delete the last element and return the value; 2, use "end ($arr)", return the value of the last element; 3, use "array_slice ($arr,-1)"; 4, use "$arr [count ($arr)-1]", and so on.

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

The method of getting the value of the Last element of an Array by php

1. Use the array_pop () function

The array_pop () function deletes the last element in the array and returns the deleted element.

2. Use the end () function

The end () function points the pointer inside the array to the last element of the array and returns the value of the last element, or FALSE if the array is empty.

4. Use the array_slice () function

To intercept the last few elements of the array, simply set the parameter start to-1

4. 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 element simply by setting the function's second argument, start, to-1.

5. Use the array name [subscript] to access the last array element

The subscript value needs to be set to array length-1, that is, count ($arr)-1

Note: this method can only be used in indexed arrays.

This is the end of the content of "how to find the value of the last element of an array by php". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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