note 94234 deleted from ref.array by danbrown
| From: | danbrown@php.net | Date: | Fri, 23 Oct 2009 19:52:04 +0000 |
| Subject: | note 94234 deleted from ref.array by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-162028@lists.php.net to get a copy of this message | ||
Note Submitter: talik at professionalscallywag dot com
----
A minor improvement to elkabong's array_deep_copy function to allow for easier inline use by
using a return value rather than requiring the result array to be passed into the function.
<?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.
never call this function with $depth variable specified.
*/
function array_copy (&$array, &$copy = false, $maxdepth=50, $depth=0) {
if($depth > $maxdepth) { $copy = $array; return; }
if(!is_array($copy))
{
$returnCopy = true;
$copy = array();
}
foreach($array as $k => &$v) {
if(is_array($v)) { array_copy($v,$copy[$k],$maxdepth,++$depth);
} else {
$copy[$k] = $v;
}
}
if($returnCopy)
return $copy;
}
//call it like:
$newArray = array_copy($oldArray);
//or
$newArray = array_copy($oldArray, false, $maxDepth);
//or
$newArray = array();
array_copy($oldArray, $newArray, $maxDepth);
?>