当前位置:网站首页>通过&修改数组中的值
通过&修改数组中的值
2022-08-04 05:25:00 【eleven_-11】
下面就直接放出代码,针对使用递归删除数组中的值,存在空数组时,可以过滤空数组,具体实现可以参考以前写的文章,这篇主要介绍’&'处理数组
<?php
$arrTest = array(
'A' => [
'An' => 'anan',
'Bn' => 'bnbn',
],
'C' => [
'Cn' => 'cncn',
'Dn' => 'dndn',
],
);
/**
* 修改数组中的值
* @param $arrToChange array 需要处理的数组
* @return array 返回处理后的数组
*/
function toChangeValue($arrToChange)
{
//使用'&'引用每次遍历过后的值,这样修改的就是原数组的值
foreach ($arrToChange as &$value) {
foreach ($value as &$each) {
if ($each == 'anan') {
$each = 'enen';
}
}
}
return $arrToChange;
}
/**
* 通过递归和'&'删除数组中不符合要求的值
* @param $arrToDelete array 需要处理的数组
* @return array 返回处理后的数组
*/
function toDeleteValue(&$arrToDelete)
{
foreach ($arrToDelete as $key => $value) {
if (is_array($value)) {
//将递归过后的数组地址传值给原数组, 这样才能删除掉原数组中的值,而不是删除复制数组中的值
$arrToDelete[$key] = toDeleteValue($value);
} elseif ($value == 'anan') {
unset($arrToDelete[$key]);
}
}
return $arrToDelete;
}
边栏推荐
猜你喜欢

3面头条,花7天整理了面试题和学习笔记,已正式入职半个月

Tactile intelligent sharing - SSD20X realizes upgrade display progress bar

部署LVS-DR群集【实验】

The cost of automated testing is high and the effect is poor, so what is the significance of automated testing?

C1认证之web基础知识及习题——我的学习笔记

Can 't connect to MySQL server on' localhost3306 '(10061) simple solutions

Grain Mall - Basics (Project Introduction & Project Construction)

Unity动画生成工具

读者让我总结一波 redis 面试题,现在肝出来了

入坑软件测试的经验与建议
随机推荐
MySQL database (basic)
败给“MySQL”的第60天,我重振旗鼓,四面拿下蚂蚁金服offer
[Evaluation model] Topsis method (pros and cons distance method)
4.3 基于注解的声明式事务和基于XML的声明式事务
擎朗智能全国研发创新中心落地光谷:去年曾获2亿美元融资
4.2 声明式事务概念
力扣:70. 爬楼梯
MySQL log articles, binlog log of MySQL log, detailed explanation of binlog log
The idea setting recognizes the .sql file type and other file types
自动化测试的成本高效果差,那么自动化测试的意义在哪呢?
7. Execution of special SQL
4.1 声明式事务之JdbcTemplate
FPGA学习笔记——知识点总结
The symbol table
The 2022 PMP exam has been delayed, should we be happy or worried?
力扣:343. 整数拆分
腾讯136道高级岗面试题:多线程+算法+Redis+JVM
DP4398:国产兼容替代CS4398立体声24位/192kHz音频解码芯片
CentOS7 - yum install mysql
7.16 Day22---MYSQL(Dao模式封装JDBC)