Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.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-“文件”;未定义索引“;请注意,在引用(&;)关联数组的未实例化元素时未引发此问题_Php_Arrays_Indexing_Reference_Undefined - Fatal编程技术网

PHP-“文件”;未定义索引“;请注意,在引用(&;)关联数组的未实例化元素时未引发此问题

PHP-“文件”;未定义索引“;请注意,在引用(&;)关联数组的未实例化元素时未引发此问题,php,arrays,indexing,reference,undefined,Php,Arrays,Indexing,Reference,Undefined,我从以下内容查看此代码: 仅感谢对$temp=&$temp[$key]的改进,其结果是: $res = [ "item1" => [ "item2" => [ "itemx" => & null ]]] 我不明白为什么&temp[$key]实例化关联[$key=>null]而$temp[$key]没有 我做了一些调试,对于第一个$key(项目1): $a=$temp[$key]

我从以下内容查看此代码:

仅感谢
$temp=&$temp[$key]
的改进,其结果是:

$res = [
  "item1" => [
    "item2" => [
      "itemx" => & null
    ]]]
我不明白为什么
&temp[$key]
实例化关联
[$key=>null]
$temp[$key]
没有

我做了一些调试,对于第一个
$key
(项目1):

  • $a=$temp[$key]:
    • 给出“未定义索引”通知
    • 转储
      $a
      返回空值
    • 转储
      $temp
      返回空值
  • $a=&$temp[$key]:
    • 不发出“未定义索引”通知
    • 转储
      $a
      返回空值
    • 转储
      $temp[$key]
      返回null
    • 转储
      $temp
      返回:
      [“item1”=>&null]
  • 这意味着,
    $temp=&$temp[$key]
    也实例化$temp[$key],或者在其前面加上以下等效项:

    $temp = &temp[$key]; <=> $temp[$key] = null; $temp = &temp[$key];
    
    $temp=&temp[$key]$临时[$key]=null$temp=&temp[$key];
    
    我想了解php文档中是否解释了这一点(我搜索过,但没有找到任何东西),或者我是否遗漏了一些明显的东西


    谢谢

    文档中对此进行了解释。创建引用时会创建未定义的变量:

    注意
    如果通过引用指定、传递或返回未定义的变量,将创建该变量

    示例#1使用带有未定义变量的引用

    <?php
    function foo(&$var) { }
    
    foo($a); // $a is "created" and assigned to null
    
    $b = array();
    foo($b['b']);
    var_dump(array_key_exists('b', $b)); // bool(true)
    
    $c = new StdClass;
    foo($c->d);
    var_dump(property_exists($c, 'd')); // bool(true)
    ?>
    
    
    

    这正是我要找的!非常感谢。
    <?php
    function foo(&$var) { }
    
    foo($a); // $a is "created" and assigned to null
    
    $b = array();
    foo($b['b']);
    var_dump(array_key_exists('b', $b)); // bool(true)
    
    $c = new StdClass;
    foo($c->d);
    var_dump(property_exists($c, 'd')); // bool(true)
    ?>