Home  >  Article  >  Backend Development  >  How to remove specific elements from an array in php

How to remove specific elements from an array in php

青灯夜游
青灯夜游Original
2022-10-10 18:02:543649browse

Method: 1. Use unset() to remove the element with the specified key name, the syntax is "unset($array name[key name])"; 2. Use array_splice() to delete multiple elements starting at the specified position, Syntax "array_splice(array, starting position, number of elements)"; 3. Use array_shift() to delete the first element, the syntax "array_shift(array)"; 4. Use array_pop() to delete the last element, the syntax "array_pop( array)".

How to remove specific elements from an array in php

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

Method 1: Use The "$array name[key name]" statement and the unset() function remove an element of the specified key name

  • $array name[key name] The statement can access an element of the specified key name

  • and the unset() function can delete an array element of the specified subscript (key name), that is, delete the accessed element

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);   
  $arr = array("Name"=>"Peter","Age"=>"41","Country"=>"USA");
  echo "原数组:";
  var_dump($arr);
  echo "删除指定键名的一个元素后:" ;
  unset($arr["Age"]);
          //↑ 你想删除的key(下标)
  var_dump($arr);
?>

How to remove specific elements from an array in php

Method 2: Use the array_splice() function to delete one or more elements starting at the specified position

array_splice() is a powerful function with multiple functions: it can insert array elements, replace array elements, and of course, it can also delete array elements (after all, the job of the array_splice() function is to delete specified elements. and replaced with other values).

The array_splice() function can delete the specified number of elements starting from the specified position.

array_splice($array,$start,$length)
Parameters Description
array 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.

array_splice()函数的参数说明:

array_splice($arr,$start)会删除从$start位置开始的所有元素删除。

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

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

How to remove specific elements from an array in php

$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);

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

How to remove specific elements from an array 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);

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

How to remove specific elements from an array in php

$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);

echo "删除后的数组:" ;
array_splice($arr,2,-1);
var_dump($arr);
?>

How to remove specific elements from an array in php

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

方法3:使用array_shift()函数删除第一个元素

array_shift() 函数用于删除数组中的第一个元素,并返回被删除的元素。

array_shift($arr)

array_shift()函数在删除$arr数组的开头的第一个元素后,arr 数组的长度会减 1,并将所有其他元素向前移动一位。如果键名是数字的,所有元素都将获得新的键名,从 0 开始,并以 1 递增;但字符串键名将保持不变。

注:该函数会改变原数组

<?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);
?>

How to remove specific elements from an array in php

可以看到这个例子,我们原先的$arr数组里面有5个元素,使用array_shift($arr)方法之后,重新使用var_dump($arr)输出数组,发现只有4个元素了,数组头部元素被删除了。

方法4:使用array_pop() 函数删除最后一个元素

array_pop()函数删除数组中的最后一个元素。

array_pop($array)

该函数会改变原数组;返回值为数组的最后一个值,如果数组是空的,或者不是一个数组,将返回 NULL。

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);  
$arr = array(1,2,3,4,5,6,7,8,9,10);
echo "原数组:";
var_dump($arr);

array_pop($arr);
echo "移除最后一项后:";
var_dump($arr);
?>

How to remove specific elements from an array in php

注:利用array_slice()函数也可实现删除第一个元素和最后一个元素。

推荐学习:《PHP视频教程

The above is the detailed content of How to remove specific elements from an array 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