Home >Backend Development >PHP Problem >How to remove the current element from a php array

How to remove the current element from a php array

青灯夜游
青灯夜游Original
2022-08-22 17:47:421568browse

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]);"

How to remove the current element from a php array

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

How to remove the current element from a php array

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

How to remove the current element from a php array

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!

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