Home  >  Article  >  Backend Development  >  How to delete the first element of php array

How to delete the first element of php array

青灯夜游
青灯夜游Original
2021-06-04 18:44:574182browse

php method to delete the first element in the array: 1. Use the array_shift() function, the syntax "array_shift(array);"; 2. Use the array_splice() function, the syntax "array_splice(array,0, 1);".

How to delete the first element of php array

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

php delete array The first element

1. Use the array_shift() function

PHP array_shift() function is used to delete the elements at the beginning of the array. The syntax is as follows:

array_shift(array)

The parameter arr represents the array to be processed.

array_shift() function will delete the first element at the beginning of the arr array and return it as the result. The length of the arr array is decremented by 1 and all other elements are moved forward by one. All numeric key names will be changed to start counting from 0, and string key names will remain unchanged.

Return value: Returns the value of the element removed from the array, or NULL if the array is empty.

Example: Delete the first element in the array

<?php
header("Content-type: text/html; charset=utf-8");
$num = array(10, 45, 9, 100, 6);
array_shift($num);  //删除数组开头的第一个元素
print_r($num);

echo "<br>";

$info = array("PHP教程", 4=>"php中文网", "http://www.php.cn",);
array_shift($info);
print_r($info);
?>

The result of executing the above program is:

Array ( [0] => 45 [1] => 9 [2] => 100 [3] => 6 )
Array ( [0] => php中文网 [1] => http://www.php.cn )

2. Use the array_splice() function

PHP array_splice() function is used to delete part of the elements of the array; you can delete them directly or replace them with other values.

array_splice() syntax is as follows:

array_splice(array1,start,length,array2)
Parameters Description
array1 Required. Specifies an array.
start Required. numerical value. Specifies the starting position of deleted elements. 0 = first element. If the value is set to a positive number, removal begins at the offset specified by the value in the array. If the value is set to a negative number, removal begins at the offset specified by the value from the end of the array. -2 means start from the second to last element of the array.
length Optional. numerical value. Specifies the number of elements to be removed, which is also the length of the returned array. If this value is set to a positive number, remove this number of elements. If this value is set to a negative number, all elements from start to length inverse of the end of the array are removed. If this value is not set, all elements from the position set by the start parameter to the end of the array are removed.
array2 Optional. Specifies the array with the elements to be inserted into the original array. If there is only one element, it can be set to a string and does not need to be set to an array.

Tip: If the function does not remove any elements (length=0), the replacement array will be inserted from the position of the start parameter.

Return value: Returns the array containing the extracted elements.

Example: Delete the first element in the array

<?php
$arr = array("red", "green", "blue", "yellow");
array_splice($arr, 0,1);
print_r($arr);
?>

Output:

Array ( [0] => green [1] => blue [2] => yellow )

Recommended learning: "PHP Video Tutorial"

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

Statement:
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