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
曾经蜡笔没有小新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;
}
过去多啦不再A梦2017-05-18 10:47:59
Just assign the value of b=a
就好了,在php
中数组不加&符号都是值传递,所以if
里面就算b
变化了,也不会影响a
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)
;