note 71119 added to ref.array
| From: | elkabong at samsalisbury dot co dot uk | Date: | Mon, 13 Nov 2006 11:43:18 +0000 |
| Subject: | note 71119 added to ref.array | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-119653@lists.php.net to get a copy of this message | ||
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!
----
Server IP: 83.138.144.80
Probable Submitter: 82.108.42.131
----
Manual Page -- http://www.php.net/manual/en/ref.array.php
Edit -- https://master.php.net/note/edit/71119
Del: integrated -- https://master.php.net/note/delete/71119/integrated
Del: useless -- https://master.php.net/note/delete/71119/useless
Del: bad code -- https://master.php.net/note/delete/71119/bad+code
Del: spam -- https://master.php.net/note/delete/71119/spam
Del: non-english -- https://master.php.net/note/delete/71119/non-english
Del: in docs -- https://master.php.net/note/delete/71119/in+docs
Del: other reasons-- https://master.php.net/note/delete/71119
Reject -- https://master.php.net/note/reject/71119
Search -- https://master.php.net/manage/user-notes.php