search

Home  >  Q&A  >  body text

About PHP reference passing problem

An array a itself is referenced by other arrays. Now I want to assign array a to array b in an if judgment, but I don’t want to assign the reference in a to b, I just want to simply pass the value. , how to do this? Because we only want to change the value of b in the if, we judge that the value of a outside cannot be changed

大家讲道理大家讲道理2748 days ago496

reply all(3)I'll reply

  • 为情所困

    为情所困2017-05-18 10:47:59

    Write an example, the text description seems a bit convoluted

    reply
    0
  • 曾经蜡笔没有小新

    曾经蜡笔没有小新2017-05-18 10:47:59

    "Assign array a to array b in an if judgment", do you assign array a to array b, or array b to array a?

    <?php
    $a = &$c;
    $d = $a;
    

    Assign array a to array b

    if(condition)
    {
        $b = $d;
    }
    

    Assign array b to array a

    if(condition)
    {
        $a = $b;
        
    }
    

    reply
    0
  • 过去多啦不再A梦

    过去多啦不再A梦2017-05-18 10:47:59

    Just assign the value of b=a就好了,在php中数组不加&符号都是值传递,所以if里面就算b变化了,也不会影响a

    directly

    It won’t change, because a is assigned by reference, which is no problem, but as long as a and b are passed by value, they will not have a reference relationship. You didn’t read the code. I tried to simulate a comparison based on your needs. Simple scenario, you can try running it yourself:

    <?php                                                                                                                                                                       
    define('CLI_SCRIPT', true);                                                                                                                                                 
                                                                                                                                                                                
    $data = array(1, 2, 3, 4);                                                                                                                                                  
    foreach ($data as &$tmp) {                                                                                                                                                  
        $a = $tmp;                                                                                                                                                              
        if (in_array($a, [1, 2])) {                                                                                                                                             
            $b = $a;                                                                                                                                                            
            $b = $b - 1;                                                                                                                                                        
            var_dump($a);                                                                                                                                                       
            var_dump($b);                                                                                                                                                       
        }                                                                                                                                                                       
    } 

    As long as you are here$b=$a之前加一句unset($val);

    reply
    0
  • Cancelreply