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
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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.