isset with variable array key count

前端 未结 1 1099
粉色の甜心
粉色の甜心 2021-01-16 18:00

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

$arr[$a][$b][$c] = \'X\';

3 in this case. I want to create a function

相关标签:
1条回答
  • 2021-01-16 19:00

    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]);
        }
    }
    
    0 讨论(0)
提交回复
热议问题