Make a copy of an object's value without copying the reference
Asked Answered
D

7

20

I have the following code:

$data['x'] = $this->x->getResults();  

$data['y'] = $data['x'];

//some code here to modify $data['y']
//this causes (undesirably) $data['x] to be modified as well

I guess since all the elements of $data are themselves references, modifying $data['y'] also modifies $data['x'] which is NOT what I want. I want $data['x'] to remain the same. Is there any way to dereference the elements here so that I can copy the elements by value?

Update:

$this->x->getResults(); returns an object array. So I can then do something like: $data['x'][0]->date_create ...

Update: my latest attempt to clone the array looks something like this:

$data['x'] = $this->x->getResults();     
$data['y'] = $data['y'];
foreach($data['x'] as $key=>$row) {
    $data['y'][$key]->some_attr = clone $row->some_attr;
}

Am I way off here? I keep getting a "__clone method called on non-object" error. From reading the responses it seems like my best option is to iterate over each element and clone it (which is what I was trying to do with that code..).

Dori answered 27/7, 2009 at 19:11 Comment(5)
What is $this->x->getResults() returning? An object?Cerise
It would be important to know what $this->x->getResults() returns...Strand
yes, I am using codeigniter so that is a call to a model (x) which returns an object array of database query resultsDori
Then you must clone every object in that array of objects.Cerise
When writing your own classes, internal variable references are not cloned by default. You must implement the clone function, eg: public function __clone() { $this->widget = clone $this->widget(); } for each reference inside your object that should be cloned as well.Infantile
V
11

You can take advantage of the fact that PHP will dereference the results of a function call.

Here's some example code I whipped up:

$x = 'x';
$y = 'y';
$arr = array(&$x,&$y);
print_r($arr);

echo "<br/>";
$arr2 = $arr;
$arr2[0] = 'zzz';
print_r($arr);
print_r($arr2);

echo "<br/>";
$arr2 = array_flip(array_flip($arr));
$arr2[0] = '123';
print_r($arr);
print_r($arr2);

The results look like this:

Array ( [0] => x [1] => y )
Array ( [0] => zzz [1] => y ) Array ( [0] => zzz [1] => y )
Array ( [0] => zzz [1] => y ) Array ( [0] => 123 [1] => y ) 

You can see that the results of using array_flip() during the assigment of $arr to $arr2 results in differences in the subsequent changes to $arr2, as the array_flip() calls forces a dereference.

It doesn't seem terribly efficient, but it might work for you if $this->x->getResults() is returning an array:

$data['x'] = array_flip(array_flip($this->x->getResults()));
$data['y'] = $data['x'];

See this (unanswered) thread for another example.

If everything in your returned array is an object however, then the only way to copy an object is to use clone(), and you would have to iterate through $data['x'] and clone each element into $data['y'].

Example:

$data['x'] = $this->x->getResults();
$data['y'] = array();
foreach($data['x'] as $key => $obj) {
    $data['y'][$key] = clone $obj;
}
Veda answered 27/7, 2009 at 19:58 Comment(4)
I am having some trouble with this: like you mentioned in your last sentence, everything in my returned array is an object..could you maybe post some code showing how to iterate over the array and clone each element? I keep getting a "_clone method called on non-object" error. Thanks.Dori
No problem, I added an example. clone only works on actual objects, so if the return result of $this->x->getResults(); is an array of objects instead of an object, then you can't clone the array, you have to clone each element of the array separately.Veda
array_flip is not a good idea!! If you have 2 keys with the same value then the first key will be lost when the array_flip function is called.Paracelsus
a bit of mis-information here. the function return doesn't do a deep-dereferencing. the magic above is the array_flip(array_flip($array))..Boudoir
B
10

array_merge() can accept any number of parameters, even 1, then produce a new array. So just do following:

$new_array = array_merge($existing_array);

But actually if you want to just copy array you can simply do

// this copying data, not returns the pointer (it's PHP, not JavaScript!)    
$new_array = $existing_array;

In PHP if you really need a “pointer” to array, you can do the following:

$new_pointer_to_existing_array = &$existing_array; // use & to get pointer
Bouldin answered 3/10, 2021 at 23:38 Comment(0)
A
7

array_flip() won't work when array values are not strings nor integers. I found a simple solution, however:

$clonedArr = (array)clone(object)$arr;

This works thanks to the properties of clone on an object.

Allocate answered 19/7, 2012 at 21:44 Comment(1)
This does not work. If $arr has reference variables in it, the new array will still have them. References survive cloning.Papoose
A
3

Not simple. Read about clone

BUT! if your elements are not objects and not refence type variables you have no problem.

Example for reference types:

$v=11;
$arr[]=&$v;
Adenosine answered 27/7, 2009 at 19:16 Comment(4)
why is it that I only have a problem if the elements are objects? is it because objects are themselves references?Dori
In PHP, (as of version 5 IICRC) all objects are passed by reference by default.Ng
This answer is not quite true. It should read "If your elements are not references, you have no problem.". You can have references that are not objects.Veda
Objects aren't references, they're technically pointers, but they appear to act like references.Shuffle
A
2

If you are working with objects, you might want to take a look at clone, to create a copy of an object, instead of a reference.

Here is a very short example :

First, with an array, it works by value :

$data['x'] = array(
    'a' => 'test',
    'b' => 'glop',
);
$data['y'] = $data['x'];
$data['y'][0] = 'Hello, world!';
var_dump($data['x']); // a => test : no problem with arrays

By default, with objects, it works by reference :

$data['x'] = (object)array(
    'a' => 'test',
    'b' => 'glop',
);
$data['y'] = $data['x'];
$data['y']->a = 'Hello, world!';
var_dump($data['x']); // a => Hello, world! : objects are by ref

But, if you clone the object, you work on a copy :
I guess this is your case ?

$data['x'] = (object)array(
    'a' => 'test',
    'b' => 'glop',
);
$data['y'] = clone $data['x'];
$data['y']->a = 'Hello, world!';
var_dump($data['x']); // a => test : no ref, because of cloning

Hope this helps,

Ablution answered 27/7, 2009 at 19:17 Comment(3)
thanks for the code sample..do you know if the clone function is relatively efficient? I ask because I know that in java it is usually best to avoid this if possible.Dori
I have no idea about the efficiency, but if you need the functionnality, it doesn't really matter, does it ? (and, considering you are doing a DB query just a while before, it shouldn't be that long, in comparison with the rest of the script :-) )Ablution
I think the only inherent inefficiency would be due to the fact that you are doubling the amount of memory in order to store the same object twice. Thus, it is only as inefficient as copying any other variable would be.Soidisant
S
1

I just discovered that if you simply want a copy of an array of values (no references) from a constant then you can just write:

$new_array = (array) (object) self::old_array;

Not an exact answer to the OP's question but it helped me and might help someone else.

Stelliform answered 3/7, 2017 at 17:36 Comment(0)
G
0

You could use this function to copy multidimensional arrays containing objects.

<?php
function arrayCopy( array $array ) {
    $result = array();
    foreach( $array as $key => $val ) {
        if( is_array( $val ) ) {
            $result[$key] = arrayCopy( $val );
        } elseif ( is_object( $val ) ) {
            $result[$key] = clone $val;
        } else {
            $result[$key] = $val;
        }
    }
    return $result;
}
?>
Gustavogustavus answered 5/5, 2012 at 13:25 Comment(2)
Doesn't clone do a shallow copy? So it won't recurse to any array properties.Enisle
Yes it won't. So i guess if there are any array properties containing some more objects you could use __clone() to let the objects themselves figure out how exactly they should be cloned.Gustavogustavus

© 2022 - 2024 — McMap. All rights reserved.