How to clone an array of objects in PHP?

DisgruntledGoat picture DisgruntledGoat · Jun 21, 2011 · Viewed 74.4k times · Source

I have an array of objects. I know that objects get assigned by "reference" and arrays by "value". But when I assign the array, each element of the array is referencing the object, so when I modify an object in either array the changes are reflected in the other.

Is there a simple way to clone an array, or must I loop through it to clone each object?

Answer

erani picture erani · Jul 17, 2012
$array = array_merge(array(), $myArray);