Home > Backend Development > PHP Problem > How to delete first element in PHP array

How to delete first element in PHP array

PHPz
Release: 2023-04-26 09:45:55
Original
698 people have browsed it

In PHP, array is a very common data structure. An array is composed of an ordered sequence of key-value pairs, and values ​​can be accessed by key name. When we operate on an array, sometimes we need to delete one of its elements. In this article, I will tell you how to delete the first element from an array in PHP.

Deleting the first element in the PHP array is actually deleting the first key-value pair of the array, because the elements of the array are all ordered. In PHP, there are various ways to delete elements from an array. Here are two commonly used methods:

The first method: use the array_shift function

The array_shift function in PHP can be used to delete the first element of the array. This function returns the value of the first element in the array and moves the array pointer to the next element.

The sample code is as follows:

<?php
$arr = array("a", "b", "c", "d", "e");
$deletedElement = array_shift($arr); // 删除数组的第一个元素,并返回它的值
print_r($arr);
?>
Copy after login

The output result is:

Array ( [0] => b [1] => c [2] => d [3] => e )
Copy after login

In the above code, we first create an array containing 5 elements. We then use the array_shift function to delete the first element of the array and assign the returned value to the $deletedElement variable. Finally, we output the modified array.

Second method: Use the unset function

In addition to using the array_shift function, you can also use the unset function to delete any element in the array, including the first element. The unset function deletes the specified array element and also rebuilds the array index.

The sample code is as follows:

<?php
$arr = array("a", "b", "c", "d", "e");
unset($arr[0]); // 删除数组的第一个元素
print_r($arr);
?>
Copy after login

The output result is:

Array ( [1] => b [2] => c [3] => d [4] => e )
Copy after login

In the above code, we use the unset function to delete the first element of the array, which is $arr [0]. Note that this method rebuilds the array index and may not be as efficient as the array_shift function.

Summary

To delete the first element in a PHP array, you can use the array_shift function or the unset function. The array_shift function is more efficient, while the unset function can delete any array element. In actual development, we need to choose an appropriate method to delete elements in the array as needed.

The above is the detailed content of How to delete first element in PHP array. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template