简体   繁体   English

PHP参考概念与数组

[英]php reference concept with array

How &`` symbol works for arrays in PHP? &``符号如何在PHP中用于数组?

When I execute below code 当我执行以下代码

<?php
    $cfg=array();
    $curpath=array();
    $name="check";
    array_push($curpath, strtolower($name));
    $ptr =& $cfg;
    /*what happens here*/
    $ptr =& $ptr[$name];

    print("\ncfg\n");
    print_r($cfg);
 ?>

Output below after execution of above code: 执行以上代码后,输出如下:

cfg
Array ( [check] => )  

Please explain below statements 请解释以下声明

 $ptr = &$cfg;
/*what happens here*/
 $ptr =& $ptr[$name];

Here, the & means that the array is assigned by reference. 在这里, &表示该数组是通过引用分配的。 This means that instead of copying the values from $cfg to $ptr , both of these actually refer to the exact same array. 这意味着,不是将值从$cfg复制到$ptr ,这两个实际上都指向完全相同的数组。 If you modify the array using $ptr , you will see those changes when you access the array using $cfg . 如果使用$ptr修改阵列,则使用$cfg访问阵列时将看到这些更改。 Likewise, if you modify the array using $cfg , you will see the changes when you access it with $ptr . 同样,如果使用$cfg修改数组,则使用$ptr访问该数组时将看到更改。

Without the & in $ptr = &$cfg; $ptr = &$cfg;不带& , the values of $cfg would be copied to $ptr . ,则$cfg的值将被复制到$ptr In that case, you would have two completely different arrays. 在这种情况下,您将拥有两个完全不同的阵列。 Then, changes to $ptr would not be reflected in $cfg and vice versa. 这样,对$ptr更改将不会反映在$cfg ,反之亦然。

For example, if we have: 例如,如果我们有:

$cfg = ["item 1" => 1, "item 2" => 2];
$ptr = &$cfg;

$ptr["item 1"] = 999;

echo $cfg["item 1"];
echo $ptr["item 1"];

The output will be: 输出将是:

999 999 999 999

But if you change $ptr = &$cfg to $ptr = $cfg , then the output will be: 1 999 但是,如果将$ptr = &$cfg更改$ptr = &$cfg $ptr = $cfg ,则输出将为:1 999

The original $cfg remains unmodified in the second case. 在第二种情况下,原始$ cfg保持不变。

Your example does not really make sense to me but lets see: 您的示例对我而言真的没有任何意义,但让我们看看:

$config = array();
$currentPath = array();

// you pushed "check" into the end of the $currentPath array
$name = "check";
array_push($currentPath, strtolower($name));

echo "currentPath:\r\n";
var_dump($currentPath);
// array (
//     0 => 'check',
// )

echo "config:\r\n";
var_dump($config);
// array(0) {
// }


// $ptr (pointer) is actually a reference.
// you now have a $reference to $config.
// (btw it should be written $foo = &$bar and NOT $foo =& $bar)
$reference = &$config;


// "what happens here"
// $reference is empty. so $reference["check"] does not exist.
// you create an offset "check" at $reference, so at $config.
// (i have no idea how - if somebody know why please let me know)
$reference = &$reference[$name];

// what happen here is
// - offset $config["check"] created
// - the reference of $config["check"] has been assigned to $reference (overwritten previous $reference var)

echo "config:\r\n";
var_dump($config);
// array(1) {
//     ["check"]=>
//   &NULL
// }


// $reference points now to $config["check"], so ...
$reference = 123;
// should set $config["check"] to 123

echo "config:\r\n";
var_dump($config);
// array(1) {
//     ["check"]=>
//   &int(123)
// }

A more "realistic" example could be: 一个更“现实”的示例可能是:

// empty config
$config = array();
// ...
// create config offset "check" with default value NULL
$name = 'check';
$config[$name] = null;
// ...

var_dump($config);
// array(1) {
//     ["check"]=>
//   NULL
// }

// using a reference to $config["check"] (for w/e reasons)
$check = &$config[$name];
// ...
// update $config["check"]
$check = 123;

var_dump($config);
// array(1) {
//     ["check"]=>
//   &int(123)
// }

unset($check); // release reference

var_dump($config);
// array(1) {
//     ["check"]=>
//   int(123)
// }

@all Why does PHP creates the offset $config["check"] when executing $reference = &$reference[$name]; @all为什么在执行$reference = &$reference[$name];时PHP创建偏移量$config["check"] $reference = &$reference[$name]; ? And shouldnt PHP bring up a undefined index notice? PHP是否应该显示未定义的索引通知?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM