So $array is an array of which all elements are references.
I want to append this array to another array called $results (in a loop), but since they are references, PHP copies the references and $results is full of identical elements.
So far, the best working solution is:
$results[] = unserialize(serialize($array));
which I fear to be incredibly inefficient. Is there a better way to do this?
You can use the fact that functions dereferences results when returning, for exemple here
$array_by_myclonewill still have a reference to$original($array_by_myclone[0][0] == 'foo') while$array_by_assignmentwill have a cloned value ($array_by_assignment[0][0] == 'bar')EDIT: I wanted to check if the comment saying
unserialize(serialize())was faster was right so I did the test using php 5.5, and it turns out this is wrong: using the serialization method is slower even with a small dataset, and the more data you have the slower it gets.Code used: