Jump to content

How to copy an array?


lordvader

Recommended Posts

When I'm copying one multi-d array to another var, acting on the second var also acts on the original var...

 

$first = array();

$first = some_func_that_returns_an_array();

$second = array();

$second = $first;

 

some_func_that_processes_array_data($second);

 

----

at which point,

print_r($first) looks identical to print_r($second) but as you can see I've done nothing to the array $first.

 

How do I copy a multi-dimensional array to another variable?

TIA

Link to comment
https://forums.phpfreaks.com/topic/98258-how-to-copy-an-array/
Share on other sites

that is how you copy an array. are you sure anything is happening to either array upon calling some_func_that_processes_array_data($second);

 

unless you have the arrays defined as globals in the function, nothing will be affected outside the function's scope. you'd have to do something like

 

$second = some_func_that_processes_array_data($second);

 

here's an example i tested for reason...

 

function foo(){
  $x = array('1','2','3');
  return $x;
}
function bar($arr){
  array_pop($arr);
  return $arr;
}

$first = array();
$first = foo();
$second = array();
$second = $first;

$second = bar($second);

print_r($first);
print_r($second);

Link to comment
https://forums.phpfreaks.com/topic/98258-how-to-copy-an-array/#findComment-502830
Share on other sites

Thank you for the reply, Ben. Actually it's not a function that accepts $second as an argument, I just wrote it that way to simplify the post. It seems that it matters, according to your response.

 

Let me rephrase:

 

function something() {

$first = array();

$first = some_func_that_returns_an_array();

$second = array();

$second = $first;

 

[Do things to $second]

 

print_r($first)

print_r($second)

}

 

Running something() shows the arrays as being identical, with $first picking up the changes done to $second.

The temporary workaround for me is to run some_func_that_returns_an_array() a second time, to assign it's output to $second.

I can't do that in production though, because some_func_that_returns_an_array() is quite time consuming.

 

 

Link to comment
https://forums.phpfreaks.com/topic/98258-how-to-copy-an-array/#findComment-502860
Share on other sites

The code at no time, refers or uses $first in any way, other than initially to make a copy.

The rest of the code is a giant

foreach $second as $key=>$val

 

and cycles through $second to assign new values to subkey 'value', like this:

 

if (such and such conditon) $second[$key]->value = "new data";

 

print_r looks lke this:

array
(
[0] => stdClass Object
  (
    [id] => 1
    [key] => name1
    [value] => phrase1
)
[1] => stdClass Object
  (
    [id] => 2
    [key] => name2
    [value] => phrase2
)
)

 

The goal is for array $second to have all the same [id] and [key] as array $first, but all new [value].

So $first is definitely untouched ... and yet, it's not!

Any ideas? TIA

Link to comment
https://forums.phpfreaks.com/topic/98258-how-to-copy-an-array/#findComment-502885
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.