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
Objects are passed by pointed by default and are not always easy to clone especially as they may have circular references. You would be better suited with a different choice of data structures.
For those providing solutions to shallow copy the easier way is this:
$b = (array)$a;
For deep copies I do not recommend this solution:
$nuarr = json_decode(json_encode($array));
This is for a deep copy. It only supports a subset of PHP types and will swap objects to array or arrays to objects which might not be what you want as well as potentially corrupting binary values and so on.
If you make a manual recursive function for deep copies the memory usage will be much less afterwards for scalar values and keys so using json or any serializer an impact beyond its point of execution.
It may be better to use unserialize(serialize($a)) for deep copies if performance is not a concern which has wider support for things such as objects though I would not be surprised if it breaks for circular references and several other unusual things.
array_merge_recursive or array_walk_recursive can also be used for arrays.
You can easily create your own recursive function that uses is_object and is_array to choose the appropriate means of copying.