PHP | Clone arrays containing objects

Author - Webner

In PHP you need deep cloning to clone arrays containing objects.

Suppose we have $cold_items as an array of objects. To clone it appropriately so that while changing the values in one array does not impact the other one, use code like this:

$clonedArray = array();
foreach ($cold_items as $coldItem)
{
$clonedItem = clone $coldItem;//here we are cloning the item
$clonedArray[] = $clonedItem;
}
$clonedArray is now independent array.
Webner Solutions is a Software Development company focused on developing Insurance Agency Management Systems, Learning Management Systems and Salesforce apps. Contact us at dev@webners.com for your Insurance, eLearning and Salesforce applications.

Leave a Reply

Your email address will not be published. Required fields are marked *