How to clone an array of objects in PHP?

后端 未结 15 719
鱼传尺愫
鱼传尺愫 2020-12-13 17:00

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

15条回答
  •  旧巷少年郎
    2020-12-13 17:31

    As suggested by AndreKR, using array_map() is the best way to go if you already know that your array contains objects:

    $clone = array_map(function ($object) { return clone $object; }, $array);
    

提交回复
热议问题