从数组到外部变量的链接?

huangapple go评论59阅读模式
英文:

links from an array to external variables?

问题

如何从数组中引用外部变量?
例如:如果我改变了数组的值,
那么这些值应该存储在由数组元素引用的变量中。
我尝试实现这个想法,但它没有起作用 :(

根据我的想法:

 1. `echo $one` 应该输出 `1`
 2. `echo $two` 应该输出 `2`


        $one;
        $two;
        $link = [$first = &$one,  $second = &$two];
        $link[0] = 1;
        $link[1] = 2;
        echo $one;  // 没有显示 1 
        echo $two;  // 没有显示 2
英文:

How to make references to external variables from an array?
For example: if I change the array values ​​then
these values ​​should be stored in variables
referenced by array elements.
I tried to implement this idea, but it didn't work 从数组到外部变量的链接?

According to my idea:

  1. echo $one should output 1

  2. echo $two should output 2

    $one;
    $two;
    $link = [$first = &$one,  $second = &$two];
    $link[0] = 1;
    $link[1] = 2;
    echo $one;  // does not show 1 
    echo $two;  // does not show 2
    

答案1

得分: 0

你应该按照以下方式结构化你的代码:

$one;
$two;
// 如果你想让它们成为键,你没有初始化$first和$second,应该像这样
//$link = [$first => &$one,  $second => &$two]; 
$link = [&$one, &$two];
$link[0] = 1;
$link[1] = 2;
// 如果你需要将它们初始化为另一个变量,那么应该在你给$link[0]和$link[1]赋值之后执行
$first = $link[0];
$second = $link[1];
echo $one;  // 显示 1 
echo $two;  // 显示 2

echo $first;  // 显示 1 
echo $second;  // 显示 2
英文:

You should structure your code like so:

    $one;
    $two;
    //you did not initialize the $first and $second if you want them to be the keys like so
    //$link = [$first => &$one,  $second => &$two]; 
    $link = [&$one, &$two];
    $link[0] = 1;
    $link[1] = 2;
   //if you need to initialize them to another variable then do
   //And it should come after you have assigned a value to $link[0] and $link[1]
    $first = $link[0];
    $second = $link[1];
    echo $one;  // show 1 
    echo $two;  // show 2

    echo $first;  // show 1 
    echo $second;  // show 2

huangapple
  • 本文由 发表于 2023年1月6日 10:44:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/75026441.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定