Home >Backend Development >PHP Problem >php remove first element of array
php method to remove the first element of an array: 1. Use the array_shift() function to delete an element at the beginning of the array, the syntax is "array_shift($arr)"; 2. Use the array_splice() function, the syntax "array_splice($arr,0,1)"; 3. Use the array_slice() function, the syntax is "array_slice($arr,1)".
The operating environment of this article: Windows 7 system, PHP 8 version, DELL G3 computer.
Method 1: Use the array_shift() function
Create a PHP file and define an array, example:
$arr = array('apple', 'orange', 'banana', 'watermelon');
Format and output the original content of the array, example:
echo '<pre class="brush:php;toolbar:false">'; print_r($arr); echo '<pre class="brush:php;toolbar:false">';
Save the above file and preview the original array content on the screen
Use array_shift() to delete the first element in the array, example:
echo array_shift($arr);
Save the above content and preview it on the screen again, The deleted element value will be printed on the screen
#Format and output the data in the array after the first element is deleted
Save the above content and preview the final effect
Method 2: Use the array_splice() function
## The #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. Example: Delete the first element in the array<?php header("Content-type:text/html;charset=utf-8"); $arr=array(10,12,20,25,24); echo "原数组:"; var_dump($arr); array_splice($arr, 0,1); echo "删除后的数组:" ; var_dump($arr); ?>
Method 3: Use the array_slice() function
array_slice() function is used to intercept an array, that is, to extract a fragment from the array. Return value: Return the intercepted subarray.<?php header("Content-type:text/html;charset=utf-8"); $arr=array(12,20,25,24,26); echo "原数组:"; var_dump($arr); echo "删除后的数组:" ; var_dump(array_slice($arr, 1)); ?>Recommended learning: "
PHP Video Tutorial"
The above is the detailed content of php remove first element of array. For more information, please follow other related articles on the PHP Chinese website!