Home >Backend Development >PHP Problem >What are the functions in php that can delete array elements?

What are the functions in php that can delete array elements?

青灯夜游
青灯夜游Original
2022-07-19 20:01:334349browse

Delete function: 1. array_shift(), which can delete the first element; 2. array_pop(), which can delete the last element; 3. array_unique(), which can remove duplicate elements in the array; 4. array_splice(), you can delete the specified number of elements starting from the specified position, the syntax is "array_splice(array, position, number)"; 5. unset(), you can delete an array element at the specified subscript, the syntax is "unset(array name) [subscript])".

What are the functions in php that can delete array elements?

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

There are multiple array elements built into PHP that can be deleted function, let’s take a look below.

1. array_shift() function

array_shift() function is used to delete the first element in the array and return the deleted element.

Note: If the key name is numeric, all elements will get new key names, starting from 0 and increasing by 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);
?>

What are the functions in php that can delete array elements?

2. array_pop() function

array_pop() function deletes the last 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_pop($arr);

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

What are the functions in php that can delete array elements?

3. array_unique() function

array_unique() function is used to remove duplicate values ​​in the array. If two or more array values ​​are the same, only the first value is retained and the other values ​​are removed.

Note: The retained array will retain the key type of the first array item.

array_unique(array,sortingtype)
Parameters Description
array Required . Specifies an array.
sortingtype Optional. Specifies the sorting type. Possible values:
  • SORT_STRING - Default. Treat each item as a string.
  • SORT_REGULAR - Sort each item in regular order (Standard ASCII, does not change type).
  • SORT_NUMERIC - Treat each item as a number.
  • SORT_LOCALE_STRING - Treat each item as a string, based on the current locale (can be changed via setlocale()).
<?php
header("Content-type:text/html;charset=utf-8");
$arr=array(1,2,3,4,4,2,6);
echo "原数组:";
var_dump($arr);

echo "删除重复值后:" ;
var_dump(array_unique($arr));
?>

What are the functions in php that can delete array elements?

4、array_splice()函数

array_splice()是一个强大的函数,有多重功能:可以插入数组元素、可以替换数组元素,当然也可以删除数组元素(毕竟array_splice()函数的本职工作是删除指定元素并用其它值取代)。

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

What are the functions in php that can delete array elements?

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

What are the functions in php that can delete array elements?

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

What are the functions in php that can delete array elements?

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

What are the functions in php that can delete array elements?

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

5、unset()函数

unset()函数可以删除指定下标(键名)的一个数组元素

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);   
  $arr = array(0 => "a", 1 => "b", 2 => "c");
  echo "原数组:";
  var_dump($arr);
  echo "删除后的数组:" ;
  unset($arr[1]);
          //↑ 你想删除的key
  var_dump($arr);
?>

What are the functions in php that can delete array elements?

推荐学习:《PHP视频教程

The above is the detailed content of What are the functions in php that can delete array elements?. 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