lordvader Posted March 28, 2008 Share Posted March 28, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/98258-how-to-copy-an-array/ Share on other sites More sharing options...
benjaminbeazy Posted March 28, 2008 Share Posted March 28, 2008 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); Quote Link to comment https://forums.phpfreaks.com/topic/98258-how-to-copy-an-array/#findComment-502830 Share on other sites More sharing options...
lordvader Posted March 28, 2008 Author Share Posted March 28, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/98258-how-to-copy-an-array/#findComment-502860 Share on other sites More sharing options...
benjaminbeazy Posted March 28, 2008 Share Posted March 28, 2008 i guess i would need to see the actual code to be able to tell what's going on Quote Link to comment https://forums.phpfreaks.com/topic/98258-how-to-copy-an-array/#findComment-502868 Share on other sites More sharing options...
lordvader Posted March 28, 2008 Author Share Posted March 28, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/98258-how-to-copy-an-array/#findComment-502885 Share on other sites More sharing options...
sasa Posted March 28, 2008 Share Posted March 28, 2008 change line $second = $first; to foreach ($first as $k => $v)$second[$k] = clone $v; Quote Link to comment https://forums.phpfreaks.com/topic/98258-how-to-copy-an-array/#findComment-502897 Share on other sites More sharing options...
lordvader Posted March 28, 2008 Author Share Posted March 28, 2008 Holy smokes, I've never heard of clone before... but it works wonderfully! Thanks sasa! Quote Link to comment https://forums.phpfreaks.com/topic/98258-how-to-copy-an-array/#findComment-502911 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.