note 94234 added to ref.array
| From: | talik at professionalscallywag dot com | Date: | Fri, 23 Oct 2009 19:50:33 +0000 |
| Subject: | note 94234 added to ref.array | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-162027@lists.php.net to get a copy of this message | ||
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);
?>
----
Server IP: 69.147.83.197
Probable Submitter: 66.213.197.74
----
Manual Page -- http://www.php.net/manual/en/ref.array.php
Edit -- https://master.php.net/note/edit/94234
Del: integrated -- https://master.php.net/note/delete/94234/integrated
Del: useless -- https://master.php.net/note/delete/94234/useless
Del: bad code -- https://master.php.net/note/delete/94234/bad+code
Del: spam -- https://master.php.net/note/delete/94234/spam
Del: non-english -- https://master.php.net/note/delete/94234/non-english
Del: in docs -- https://master.php.net/note/delete/94234/in+docs
Del: other reasons-- https://master.php.net/note/delete/94234
Reject -- https://master.php.net/note/reject/94234
Search -- https://master.php.net/manage/user-notes.php