Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 多维数组中的unset()首先检查键是否存在_Php - Fatal编程技术网

Php 多维数组中的unset()首先检查键是否存在

Php 多维数组中的unset()首先检查键是否存在,php,Php,我的阵列: $array = array( 'test' => 'hello', 'something' => array( 'sub1' => 'foo' ) ); 我想使用: if(array_key_exists('sub1', $array['something']) { unset($array['something']['sub1']; } unset部分可以工作,但是if语句返回false,而sub1项肯定在那里。为什么会这样?我认为您

我的阵列:

$array = array(
  'test' => 'hello',
  'something' => array(
    'sub1' => 'foo'
  )
);
我想使用:

if(array_key_exists('sub1', $array['something'])
{
  unset($array['something']['sub1'];
}

unset
部分可以工作,但是if语句返回false,而
sub1
项肯定在那里。为什么会这样?

我认为您需要重新格式化代码。但是,下面是格式化的代码

 $arr = [
    'test' => 'hello',
    'something' => [
        'sub1' => 'foo'
     ]
  ];

  if(
     is_array($arr['something']) && 
     array_key_exists('sub1', $arr['something'])
   )
   {
      unset($arr['something']['sub1']);
   }

  print_r($arr);
我得到了以下结果

Array
(
    [test] => hello
    [something] => Array
        (
        )

)

你能详细说明一下你想做什么吗???

你的代码甚至没有执行,至少有4个解析错误…没有所有语法错误,代码运行良好。