使用isset与变量数组键计数 [英] isset with variable array key count

查看:101
本文介绍了使用isset与变量数组键计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

阵列是多维和具有可变数目的子密钥,像

The array is multi-dimensional and has a variable number of sub-keys, like

$改编[$一] [$ B] [$ C] ='X';

3。我想创建一个函数,如 ABC 字符串作为参数,并检查数组中有那把钥匙,然后取消它:未设置($改编[$一] [$ b] [$ C])。

3 in this case. I want to create a function that takes a string like a.b.c as argument and checks if the array has that key in it, then unset it: unset($arr[$a][$b][$c]).

如果我给它 A·B 那么就应该取消设置($改编[$一] [$ B])

if I give it a.b then it should unset($arr[$a][$b])

我倒是AP preciate任何帮助...

I'd appreciate any help...

推荐答案

下面是一个递归方法,你的问题:

Here is a recursive approach to your problem:

function removeByStr($key, &$arr)
{
    if(!is_array($key))
    {
        $key = explode(".", $key);
    }
    $i = array_shift($key);
    if(count($key) == 0)
    {
        if(!isset($arr[$i]))
        {
            return;
        }
        unset($arr[$i]);
    }
    else if(isset($arr[$i]) && is_array($arr[$i]))
    {
        removeByStr($key, $arr[$i]);
    }
}

这篇关于使用isset与变量数组键计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆