Home > Mobile >  links from an array to external variables?
links from an array to external variables?

Time:01-06

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
    

CodePudding user response:

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];
    //if you need to initialize them to another variable then do
    //$first = $link[0];
    //$second = $link[1];
    
    $link[0] = 1;
    $link[1] = 2;
    echo $one;  // show 1 
    echo $two;  // show 2
  • Related