php如何删除多维数组
php删除多维数组的方法:首先创建一个PHP示例文件;然后通过unsetMultiKeys方法删除复杂的多维数组里面的指定键值对;最后查看运行结果即可。
推荐:《PHP视频教程》
php删除多维数组里面的值
在手册里面发现,改造后变成了一个函数,可以删除复杂的多维数组里面的制定键值对!<?php
$arr = [
'test' => 'value',
'level_one' => [
'level_two' => [
'level_three' => [
'replace_this_array' => [
'special_key' => 'replacement_value',
'key_one' => 'testing',
'key_two' => 'value',
'four' => 'another value',
],
],
'ordinary_key' => 'value',
],
],
];
$unset = array('special_key', 'ordinary_key', 'four');
echo "
";
print_r(unsetMultiKeys($unset, $arr));
print_r($arr);
echo "
";exit;
function unsetMultiKeys($unset, $array) {
$arrayIterator = new \RecursiveArrayIterator($array);
$recursiveIterator = new \RecursiveIteratorIterator($arrayIterator, \RecursiveIteratorIterator::SELF_FIRST);
foreach ($recursiveIterator as $key => $value) {
foreach ($unset as $v) {
if (is_array($value) && array_key_exists($v, $value)) {
// 删除不要的值
unset($value[$v]);
// Get the current depth and traverse back up the tree, saving the modifications
$currentDepth = $recursiveIterator->getDepth();
for ($subDepth = $currentDepth; $subDepth >= 0; $subDepth--) {
// Get the current level iterator
$subIterator = $recursiveIterator->getSubIterator($subDepth);
// If we are on the level we want to change, use the replacements ($value) other wise set the key to the parent iterators value
$subIterator->offsetSet($subIterator->key(), ($subDepth === $currentDepth ? $value : $recursiveIterator->getSubIterator(($subDepth + 1))->getArrayCopy()));
}
}
}
}
return $recursiveIterator->getArrayCopy();
}
运行结果:
改变多维数组里面的键值对
php如何删除多维数组的教程已介绍完毕,更多请关注跳墙网其他文章教程!
php如何删除多维数组相关教程
关于Laradocke运行TP项目
下面由 thinkphp 框架教程栏目给大家介绍关于Laradocke运行TP项目,希望对需要的朋友有所帮助! 一、laradock首次up的时候,build过程非常慢 由于拉取容器的镜像默认在国外,所以在docker-compose up 容器之前,在.env文件中查找以下文字 CHANGE_SOURCE=``fa
如何种植屡获殊荣的青豆
如何种植屡获殊荣的青豆 Most people don’t know this yet, but I’ve decided to give up computers and become a farmer instead. Since I’m the helpful type, I’ve decided to share everything I know about farming with you, starting with how I
CC2530如何实现(1.1uA)低功耗?
CC2530如何实现(1.1uA)低功耗? ?ZigBee网络低功耗运行一直是苦逼攻城狮们要解决的问题,TI的CC2530可以说已经把终端的休眠功耗降低到了极致,现在我们就来体验这颗芯片的魅力! 文章末尾分享完整源码和PCB工程文件。 一、CC2530供电模式 CC2530芯片有五种运
recuva文件恢复乱码_使用Recuva恢复意外删除的文件
recuva文件恢复乱码_使用Recuva恢复意外删除的文件 recuva文件恢复乱码 It can be the worst feeling in the world when you accidentally delete a very important file you have been working on for days. Today we look at Recuva, a free tool that hel
删除Custom aboutconfig即可轻松输入
删除Custom about:config即可轻松输入 Have you ever added a custom about:config entry to your Firefox browser and then wished that you could remove it? Now you can with a simple reset tweak that focuses on singular entries. 您是否曾经在Fire
最新出炉头条三面技术四面HR看我如何一步一步攻克面试官
最新出炉,头条三面技术四面HR,看我如何一步一步攻克面试官? 最开始面的头条游戏中台,当时是第一次面试,没有经验,导致算法题虽然有思路,但是没有写出最优解,直接挂了。后来又被捞起来,因为此时已有阿里的offer,所以胆子也大起来了,收拾心情开始面
popd linux_如何在Linux上使用Push和Popd
popd linux_如何在Linux上使用Push和Popd popd linux Fatmawati Achmad Zaenuri/ShutterstockFatmawati Achmad Zaenuri / Shutterstock Many Linux folks have never heard of pushd and popd , but they’ve been around forever. They can also dramaticall
如何在Linux上使用join命令
如何在Linux上使用join命令 Fatmawati Achmad Zaenuri/ShutterstockFatmawati Achmad Zaenuri / Shutterstock If you want to merge data from two text files by matching a common field, you can use the Linux join command. It adds a sprinkle of dynam