Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php array assign by copying value or by reference? [duplicate]

Possible Duplicate:
are arrays in php passed by value or by reference?

I heard that PHP can select how to assign arrays, depends on array size. It can assign by copying value (as any scalar type) or by reference.

PHP always assign array to variables by copying a value, as it says in manual.

Or it can assign by reference. ?

<?php $a = array(1,2,3); ?>
like image 840
RusAlex Avatar asked Jan 17 '12 16:01

RusAlex


People also ask

Does PHP pass arrays by reference or value?

With regards to your first question, the array is passed by reference UNLESS it is modified within the method / function you're calling. If you attempt to modify the array within the method / function, a copy of it is made first, and then only the copy is modified.

Is there a function to make a copy of a PHP array to another?

1 Expert Answer Just assign the array to a variable. $array2 = $array1; If you want them to reference each other (So the change in one array will affect the other) use the & character before the variable.

Which function is used to copy all the values from array to variable in PHP?

$arr_one_copy = array_combine(array_keys($arr_one), $arr_one);

Can we pass array as argument in PHP?

Yes, you can safely pass an array as a parameter.


2 Answers

Assigning arrays by reference is possible when assigning array variables to other variables:

// By value...
$a = array(1,2,3);
$b = $a;
array_push($a, 5);
print_r($b);

// $b is not a reference to $a
Array
(
    [0] => 1
    [1] => 2
    [2] => 3
)

// By Reference
$a = array(1,2,3);
$b = &$a; // Here we assign by reference
array_push($a, 5);
print_r($b);

// $b is a reference to $a and gets the new value (3 => 5)
Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 5
)
like image 158
Michael Berkowski Avatar answered Sep 30 '22 17:09

Michael Berkowski


You can't assign something by reference unless you are referencing something that already exists. Equally, you can't copy something that doesn't exist.

So this code:

$a = array(1,2,3);

...neither copies or references - it just creates a new array fills it with values, because the values were specified literally.

However, this code:

$x = 1;
$y = 2;
$z = 3;
$a = array($x,$y,$z);

...copies the values from $x, $y and $z into an array1. The variables used to initialise the array values still exist in their own right, and can be modified or destroyed without affecting the values in the array.

This code:

$x = 1;
$y = 2;
$z = 3;
$a = array(&$x,&$y,&$z);

...creates an array of references to $x, $y and $z (notice the &). If, after running this code, I modify $x - let's say I give it a value of 4 - it will also modify the first value in the array. So when you use the array, $a[0] will now contain 4.

See this section of the manual for more information of how reference work in PHP.


1Depending on the types and values of the variables used as the array members, the copy operation may not happen at the time of the assignment even when assigned by-value. Internally PHP uses copy-on-write in as many situations as possible for reasons of performance and memory efficiency. However, in terms of the behaviour in the context of your code, you can treat it as a simple copy.

like image 23
DaveRandom Avatar answered Sep 30 '22 18:09

DaveRandom