Home  >  Article  >  Backend Development  >  How to delete elements with specified keys in an array in php

How to delete elements with specified keys in an array in php

王林
王林Original
2021-07-13 14:21:061948browse

The way PHP deletes the element with the specified key in the array is to use the unset() function to delete, such as [unset($data[0]);]. The unset() function can destroy a single variable or a single array element.

How to delete elements with specified keys in an array in php

The operating environment of this article: windows10 system, php 7.3, thinkpad t480 computer.

It is not difficult to remove the element with the specified key name in the array in PHP. It can be completed through the functions provided by PHP.

The unset() function is used to destroy the given variable. This function has no return value. Using the unset() function we can easily delete the specified element from the array.

Example:

<?php
// 销毁单个变量
unset ($foo); 
// 销毁单个数组元素
unset ($bar[&#39;quux&#39;]); 
// 销毁一个以上的变量
unset($foo1, $foo2, $foo3);
?>

Delete the element with the specified key name in the array

unset($data[0]);	
print_r($data);    
array (“1”=>1,“2”=>1);

Related video tutorial sharing: php video tutorial

The above is the detailed content of How to delete elements with specified keys in 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