About PHP reference passing problem
大家讲道理
大家讲道理 2017-05-18 10:45:59
0
3
499

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

大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

reply all(3)
为情所困

Write an example, the text description seems a bit convoluted

曾经蜡笔没有小新

"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;
    
}
过去多啦不再A梦

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);

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template