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

How to delete the first element of php array

青灯夜游
Release: 2023-03-10 11:28:01
Original
4214 people have browsed it

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)
Copy after login

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中文网", "//m.sbmmt.com",);
array_shift($info);
print_r($info);
?>
Copy after login

The result of executing the above program is:

Array ( [0] => 45 [1] => 9 [2] => 100 [3] => 6 )
Array ( [0] => php中文网 [1] => //m.sbmmt.com )
Copy after login

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)
Copy after login
ParametersDescription
array1Required. Specifies an array.
startRequired. 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.
array2Optional. 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);
?>
Copy after login

Output:

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

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!

Related labels:
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