How to remove the current element from a php array

青灯夜游
Release: 2023-03-16 18:32:01
Original
1483 people have browsed it

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

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

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($数组变量名[键名]);
Copy after login
  • $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);
?>
Copy after login

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!

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!