Home  >  Article  >  Backend Development  >  Is it possible to take the value of an array and remove it in php?

Is it possible to take the value of an array and remove it in php?

青灯夜游
青灯夜游Original
2022-09-29 20:04:253091browse

Can. 3 implementation methods: 1. Use array_shift() to delete the first element and return, the syntax is "array_shift(array)"; 2. Use array_pop() to delete the last element and return, the syntax is "array_pop(array)"; 3. Use array_splice() to delete the specified number of elements starting from the specified position and return the removed elements in the form of an array. The syntax is "array_splice(array, starting position, number of deleted elements)".

Is it possible to take the value of an array and remove it in php?

The operating environment of this tutorial: windows7 system, PHP version 8.1, DELL G3 computer

In the PHP array, you can get the specified value, and delete it from the original array.

Method 1: Use array_shift() function

array_shift() function is used to delete the first item in the array elements and returns the deleted element. (array_shift() will change the original array.)

array_shift(array)

Example:

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

$r=array_shift($arr);
echo "第一个元素 $r <br><br>";

echo "获取元素后的:" ;
var_dump($arr);
?>

Is it possible to take the value of an array and remove it in php?

You can see this example, our original $ There are 5 elements in the arr array. After using the array_shift($arr) method, use var_dump($arr) again to output the array and find that there are only 4 elements. The head element of the array has been deleted.

array_shift() 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.

Method 2: Use array_pop() function

array_pop() function deletes the last element in the array and returns the The deleted element. (array_pop() will change the original array.)

array_pop(array)

Note: If the array is empty or not an array, NULL will be returned.

Example:

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

$r=array_pop($arr);
echo "最后一个元素 $r <br><br>";

echo "获取元素后的:" ;
var_dump($arr);
?>

Is it possible to take the value of an array and remove it in php?

Method 3: Use array_splice() function

array_splice() is a powerful function with multiple functions: it can insert array elements, replace array elements, and of course, delete array elements (after all, the job of the array_splice() function is to delete the specified element and replace it with another value). Let’s take a look at its deletion function.

Delete syntax:

array_splice(array,start,length)
Parameters Description
array Required. Specifies an array.
start Required. numerical value. Specifies the starting position of deleted elements.
length Optional. numerical value. Specifies the number of elements to be removed, which is also the length of the returned array.

array_splice()函数会改变原数组,并以数组形式返回被移除元素。

我们来看下面一个小例子。

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

$r=array_splice($arr,2);
echo "删除的元素 ";
var_dump($r);

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

输出结果为:

Is it possible to take the value of an array and remove it in php?

可以看出,我们使用array_splice($arr,2)从$arr数组的第3个元素开始删除元素,共删除了3个元素(将从第3个元素开始的所有元素删除了)。array_splice($arr,$start)会删除从$start位置开始的所有元素删除,并以数组的形式返回被删除的元素。

$start参数有三种取值情况:

  • 为正数,那么从$start位置开始,往后删除;

  • 为0,那么从第一个元素开始,往后删除;

  • 为负数,则从距离 $arr 末端 -start 的位置开始,从后往前删除。例如 -2 意味着从数组的倒数第二个元素开始。

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

$r=array_splice($arr,-2);
echo "删除的元素 ";
var_dump($r);

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

输出结果为:

Is it possible to take the value of an array and remove it in php?

array_splice()函数是强大的,可以删除多个元素,也可只删除一个元素,那就需要给该函数指定一个$length参数(第三个参数),该参数用来规定删除的元素个数

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

$r=array_splice($arr,2,1);
echo "删除的元素 ";
var_dump($r);

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

输出结果为:

Is it possible to take the value of an array and remove it in php?

可以看出只删除了第三个元素“20”。

在删除元素操作中,$length参数也有三种取值情况:

  • 为正数,那么就表示删除 length 个元素;

  • 为负数,那么将删除从 start 开始,到数组末尾倒数 length 为止的所有元素;

  • 如果省略,那么将删除从 start 开始,一直到数组末尾的所有元素。

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

$r=array_splice($arr,2,-1);
echo "删除的元素 ";
var_dump($r);

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

输出结果为:

Is it possible to take the value of an array and remove it in php?

注:$length参数还可以为0,那么就表示不删除元素,可以和该函数的第四个参数$value相配合,进行插入操作(这里就不做具体介绍了)。

推荐学习:《PHP视频教程

The above is the detailed content of Is it possible to take the value of an array and remove it in php?. 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