note 71119 deleted from ref.array by levim

From: Date: Tue, 06 May 2014 22:04:50 +0000
Subject: note 71119 deleted from ref.array by levim
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-199454@lists.php.net to get a copy of this message
Note Submitter: elkabong at samsalisbury dot co dot uk ---- An improvement to the array_deep_copy function I posted ages ago which takes a 'snapshot' of an array, making copies of all actual values referenced... Now it is possible to prevent it traversing the tree forever when an array references itself. You can set the default $maxdepth to anything you like, you should never call this function with the $depth specified! <?php /* Make a complete deep copy of an array replacing references with deep copies until a certain depth is reached ($maxdepth) whereupon references are copied as-is... */ function array_deep_copy (&$array, &$copy, $maxdepth=50, $depth=0) { if($depth > $maxdepth) { $copy = $array; return; } if(!is_array($copy)) $copy = array(); foreach($array as $k => &$v) { if(is_array($v)) { array_deep_copy($v,$copy[$k],$maxdepth,++$depth); } else { $copy[$k] = $v; } } } # call it like this: array_deep_copy($array_to_be_copied,$deep_copy_of_array,$maxdepth); ?> Hope someone finds it useful!

« previous php.notes (#199454) next »