PHP array learning how to remove the first and last values ​​​​

青灯夜游
Release: 2023-03-12 06:14:01
Original
3019 people have browsed it

In the previous article, we introduced 4 methods of inserting new elements into the array. If you are interested, you can click on the link to view → "PHP Array Learning Tips to Use Functions to Insert Elements (1)" , "PHP Array Learning Skills Using Functions to Insert Elements (2)". This time we will show you how to use PHP to delete array elements. You can refer to it if you need it.

Delete if you add it. Since we introduced adding array elements before, this article will introduce deleting elements, mainly to learn how to delete elements at the beginning and end of the array. First, let's take a look at how to delete the front (head) element of the array.

Let’s take a look at the following example:

<?php
header("Content-type:text/html;charset=utf-8");
$arr=array(10,12,20,25,24);
echo "原数组:";
var_dump($arr);

array_shift($arr);

echo "删除后的数组:" ;
var_dump($arr);
?>
Copy after login

Output result:

PHP array learning how to remove the first and last values ​​​​

You can see this example , there were 5 elements in our original $arr array. After using the array_shift($arr) method, we reused var_dump($arr) to output the array and found that there were only 4 elements. , the array head element is deleted.

array_shift($arr)After the function deletes the first element at the beginning of the $arr array, the length of the arr array will be reduced by 1 and all other elements will be moved forward by one . If the key is numeric, all elements will get a new key, starting at 0 and increasing by 1; but string keys will remain unchanged.

And after we perform the deletion operation, we output the original array to observe whether the elements are deleted. It is not difficult to find that array_shift() will change the of the original array.

Okay, we have introduced the method of deleting the head element of the array. Now let’s learn about the method of deleting the tail element of the array.

<?php
header("Content-type:text/html;charset=utf-8");
$arr=array(10,12,20,25,24);
echo "原数组:";
var_dump($arr);

array_pop($arr);

echo "删除后的数组:" ;
var_dump($arr);
?>
Copy after login

Output result:

PHP array learning how to remove the first and last values ​​​​

It can be seen that there are 5 elements in our original $arr array. After using array_pop($arr )After the method, use var_dump($arr) to output the array again, and find that there are only 4 elements, and the tail element of the array has been deleted.

array_pop($arr)After the function deletes the last element of the $arr array, the length of the arr array will be reduced by 1. And array_pop() will also change the original array.

Okay, that’s all. If you want to know anything else, you can click this. → →php video tutorial

Finally, I would like to recommend a free video tutorial on PHP arrays: PHP function array array function video explanation, come and learn!

The above is the detailed content of PHP array learning how to remove the first and last values ​​​​. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!