Home >Backend Development >PHP Problem >How to remove the current element from a php array
Steps to delete the current element from the php array: 1. Use the key() function to obtain the key name of the current element, with the syntax "key($array variable name)"; 2. Use the unset() function to obtain the key To delete the current element, the syntax is "unset($array variable name[key name]);"
The operating environment of this tutorial: windows7 system, PHP version 8.1, DELL G3 computer
In php, you can use the key() function and unset() function to remove the current element.
Implementation steps:
Step 1: Use the key() function to get the key name of the current element
key() The function returns the element key name from the current internal pointer position, that is, returns the key name of the current element.
key(array)
Note: Initially, the current element points to the first element.
<?php header("Content-type:text/html;charset=utf-8"); $arr=[1,2,3,4,5,6,7,8,9]; var_dump($arr); $con=key($arr); echo "当前元素的键名为:".$con; ?>
Step 2: Use the unset() function to delete the current element based on the obtained key name
unset($数组变量名[键名]);
$Array variable name [key name]
statement is used to access the specified element based on the key name
unset() function is used to destroy the given variable
<?php header("Content-type:text/html;charset=utf-8"); $arr=[1,2,3,4,5,6,7,8,9]; var_dump($arr); $con=key($arr); echo "当前元素的键名为:".$con; unset($arr[$con]); var_dump($arr); ?>
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to remove the current element from a php array. For more information, please follow other related articles on the PHP Chinese website!