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

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

王林
Release: 2023-03-11 13:34:02
Original
2027 people have browsed it

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);
?>
Copy after login

Delete the element with the specified key name in the array

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

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!

Related labels:
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