In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "how PHP deletes the first and last elements in the array". In the daily operation, I believe many people have doubts about how to delete the first and last elements in the array by PHP. The editor consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the questions of "how PHP deletes the first and last elements in the array". Next, please follow the editor to study!
The array_shif function is built into PHP to delete the first element in the array, and the array_pop function is used to delete the last element in the array. Let's take a look at the use of these two functions respectively. Although both delete elements, these two functions are different from the unset function and the array_splice function. The two functions described in this article do not need to know the key value and key name of the array element.
Array_shif function-deletes the first element of the array
In PHP, you can delete the first array element through the array_shif function. The basic syntax format of the array_shif function is as follows:
Array_shift (array)
Among them, it should be noted that:
The parameter array represents the array that needs to be deleted. The result returned after the function runs successfully is the value of the deleted element. If the function runs unsuccessfully, the result returned is NULL. If the key name in the array is a number, the key name is reassigned after the element is deleted by the function, starting with 0, followed by 1, and so on.
Let's take a look at the use of the array_shift function through an example, as follows:
Output result:
In the above example, the first element in the array element is deleted through the array_shift function. Let's take a look at the effect on the output if the key name in the array is a number. The example is as follows:
Output result:
As you can see in the above example, when the key name in the array is a number, the key name is reassigned from 0 when the first array element is deleted by the array_shift function.
We have learned that the first element in the array can be deleted through the array_shift function, so let's take a look at how to delete the last element in the array element through the array_pop function.
Array_pop function-deletes the last element
Similar to the array_shift function in PHP, we can delete the last element in the array element through the array_pop function. The basic syntax format of the array_pop function is as follows:
Array_pop (array)
Among them, it should be noted that:
The parameter array is the array that needs to be deleted. If the function executes successfully, the result returned is the deleted element. If the array is empty or the parameter is not an array, the returned result is NULL.
Let's take a look at the use of the array_pop function through an example, as follows:
Output result:
The example above is to delete the last element in the array through the array_pop function.
When we delete the first element through the array_shif function, we know that when the key name of the array is a number, the key name will be reassigned, so when we use the array_pop function to delete the last element of the array, will the key name be reassigned if the key name of the element in the array is a number?
Let's verify it with an example to see if the array_pop function has an impact on numeric key names. The example is as follows:
Output result:
From the above example, you can find that deleting the last element in the array through the array_pop function does not reassign the key name when the number is the key name, as the array_shif function does. The array_pop function does not reassign the key name.
At this point, the study on "how to delete the first and last elements in an array by PHP" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.