当前位置:网站首页>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 .
边栏推荐
- [natural language processing] [vector representation] augsbert: improve the data enhancement method of Bi encoders for paired sentence scoring tasks
- 【论文笔记】基于在线预测和规划的机器人动态跟踪抓取方法
- IPFs of Internet Protocol
- 校验码生成
- DHCP first static experiment
- R language drawing parameters (R language plot drawing)
- PHP JSON variable array problem
- Design of regular expressions
- [tensorflow] 关于 seed
- Explain in detail the addition (+) operation in JS, basic data type addition, reference data type addition, and the underlying operation rules, [] + {}, {} + []
猜你喜欢

Network Security Learning (11) scanning and blasting

MathType安装和解决不能Crtl+V的问题

The fifth article in the series of radar Fundamentals: the function of radar modulation style

AI首席架构师12-AICA-工业生产过程优化场景下产业落地解析

Madness. MySQL learning.

Mongodb features, differences with MySQL, and application scenarios

连续三年成为云AI服务领导者,亚马逊云科技做对了什么?

QT operation to solve large amount of duplicate data

Notification(状态栏通知)详解

Analysis of direction finding error of multi baseline interferometer system
随机推荐
Zcmu--5015: complete the task
CSV generated by PHP cannot completely display the number with leading 0
Longitude and latitude and its transformation with coordinate system
【接口性能优化】索引失效的原因以及如何进行SQL优化
Opencv compile and call GPU version
QT operation to solve large amount of duplicate data
ASP date function (what if the disk function is incorrect)
四旋翼飞行器的飞控实现「建议收藏」
Ma Tiji Wanmin hall Chef
VisualBox启动虚拟机报错:The VM session was closed before any attempt to power it on.
anaconda安装教程环境变量(如何配置环境变量)
Summary of common methods of string:
Design of Butterworth filter and drawing of amplitude frequency characteristic curve
赋能合作伙伴,亚马逊云科技如何落地“扶上马,送一程”?
CSV intro
Hj9 extract non duplicate integers
技术美术百人计划学习笔记(1)--基础渲染管线
每周推荐短视频:需要协同的智能设备越来越多,给物联网开发提出更大挑战?
Stack and stack class
Experience of machine learning with Google Lab