0

Suppose I have an array of nodes (objects). I need to create a duplicate of this array that I can modify without affecting the source array. But changing the nodes will affect the source nodes. Basically maintaining pointers to the objects instead of duplicating their values.

// node(x, y)
$array[0] = new node(15, 10);
$array[1] = new node(30, -10);
$array[2] = new node(-2, 49);

// Some sort of copy system
$array2 = $array;

// Just to show modification to the array doesn't affect the source array
array_pop($array2);
if (count($array) == count($array2))
  echo "Fail";    

// Changing the node value should affect the source array
$array2[0]->x = 30;
if ($array2[0]->x == $array[0]->x)
   echo "Goal";

What would be the best way to do this?

7
  • I don't get it. How can you modify the array without affecting the source array when you want it to update the source array when you alter a value in the duplicated array? Can you give an example of what you are doing with this? Commented Mar 24, 2010 at 15:07
  • I think he means that the duplicated array should have the references to the same instances as the first array, but be itself indepent from array. So changing the contents of the second array won't modify the first array, but changing some parameter in a class referenced by the second array will modify the same instance that is refenced by the first array. Commented Mar 24, 2010 at 15:12
  • @Kjir: Ahhh, I see that makes sense now :) Commented Mar 24, 2010 at 15:16
  • @Kjir: oh well then my answer below does the opposite haha. Commented Mar 24, 2010 at 15:23
  • @prodigitalson: Then you should delete it. Commented Mar 24, 2010 at 15:25

2 Answers 2

2

If you use PHP 5:

Have you run your code? It is already working, no need to change anything. I get:

Goal

when I run it.

Most likely this is because the values of $array are already references.

Read also this question. Although he OP wanted to achieve the opposite, it could be helpful to understand how array copying works in PHP.

Update:

This behaviour, when copying arrays with objects, the reference to the object is copied instead the object itself, was reported as a bug. But no new information on this yet.


If you use PHP 4:

(Why do you still use it?)

You have to do something like:

$array2 = array();

for($i = 0; $i<count($array); $i++) {
    $array2[$i] = &$array[$i];
}
Sign up to request clarification or add additional context in comments.

2 Comments

Ahhh, I didn't actually test the code. I was expecting having to use copy or something like that. Thanks for the info. How does this work when passing through functions?
@StJohn Johnson: What do you mean with passing through functions? An object passed to a function as parameter? This should also be passed by reference (in PHP 5). See also here: devzone.zend.com/article/1714
0

it is some time that I don' write PHP code, but does the code

// Some sort of copy system
$array2 = $array;

actually work?

Don't you have to copy each element of the array in a new one?

2 Comments

No, you don't have to copy every element. The documentation says: Array assignment always involves value copying. php.net/manual/en/language.types.array.php
you are right... during my answer I was thinking how to keep pointers instead of a copy of the objects...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.