当前位置:网站首页>How does PHP remove an element from an array based on the key value
How does PHP remove an element from an array based on the key value
2022-07-25 23:09:00 【fmamcn】
php The method of removing the elements in the array according to the key value :1、 Use array_search() Function searches the array for the specified key value , And return the corresponding key name , grammar “array_search( Specified key name ,$arr,true);”;2、 Use unset() Function deletes the specified array element according to the obtained key name , grammar “unset($ Array variable name [ Key name ])”.
php The method of removing an element in the array according to the key value
Realization thought :
Get the corresponding key name according to the key value
Use the key name to access the specified array elements , And delete the element
Implementation steps :
1、 Use array_search() Function get key name
- <?php
- header('content-type:text/html;charset=utf-8');
- $arr=array("a"=>"red","b"=>"green","c"=>"blue");
- var_dump($arr);
- $key=array_search("red",$arr,true);
- echo " Corresponding key name :".$key;
- ?>

2、 Use unset() Function deletes the element according to the key name
- unset($arr[$key]);
- var_dump($arr);

explain :$ Array variable name [ Key name ] Statement to access the specified element
Expanding knowledge :
array_search() Function searches for a key value in an array , And return the corresponding key name .
- array_search(value,array,strict)
| Parameters | describe |
|---|---|
| value | It's necessary . Specify the key value to search in the array . |
| array | It's necessary . Specify the array to be searched . |
| strict | Optional . If this parameter is set to TRUE, Then the function searches the array for elements with the same data type and value . Possible value :
|
Return value : If the specified key value is found in the array , The corresponding key name is returned , Otherwise return to FALSE. If the key value is found more than once in the array , The key name matched by the key value found for the first time .
unset()Function to destroy a given variable .notes : The function does not return a value .
If in a function unset() A global variable , Only local variables are destroyed , Variables in the calling environment will remain called unset() The same value as before .
边栏推荐
- Vs2019 WinForm clr20r3 error
- Learning notes of technical art hundred people plan (1) -- basic rendering pipeline
- Notification(状态栏通知)详解
- Enabling partners, how can Amazon cloud technology "get on the horse and get a ride"?
- Thinkphp6 temporarily close the layout
- Stack and stack class
- 理解的英文(言语理解)
- 【论文笔记】A Meta-Reinforcement Learning Algorithm for Causal Discovery
- Data filtering of MATLAB
- 如何获取广告服务流量变现数据,助力广告效果分析?
猜你喜欢

Day 3 experiment

Design of Butterworth filter and drawing of amplitude frequency characteristic curve

【论文笔记】基于在线预测和规划的机器人动态跟踪抓取方法

Hcie is finally in hand, and the road begins

Deep recursion, deep search DFS, backtracking, paper cutting learning.

单模型常识推理首超人类!HFL登顶OpenBookQA挑战赛

wordpress去掉网站发布时间

DHCP first static experiment

Longitude and latitude and its transformation with coordinate system

Network Security Learning (11) scanning and blasting
随机推荐
The difference between abstract classes and interface interfaces
How painful is it to write unit tests?
Wamp MySQL empty password
The difference between overloading and rewriting
Analysis of direction finding error of multi baseline interferometer system
Ip--- ia review
Why is Google's internal tools not suitable for you?
Deploy flash based websites using Google cloud
Call Gaode map -- address is converted into longitude and latitude
PCL basic operation Encyclopedia
5 ROS仿真建模(3- rviz+gazebo+控制仿真机器人)
HJ9 提取不重复的整数
QT string operation
HJ7 取近似值
Notification设置的小图标显示的是小方块
赋能合作伙伴,亚马逊云科技如何落地“扶上马,送一程”?
asp日期函数(磁盘函数不正确怎么办)
ZCMU--5015: 完成任务
Zero crossing position search of discrete data (array)
The difference between abstract classes and interfaces