Bug #17279: array_recurse_safe()
| From: | tomat at lenderlabdot dot com | Date: | Thu, 16 May 2002 22:09:50 +0000 |
| Subject: | Bug #17279: array_recurse_safe() | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-7876@lists.php.net to get a copy of this message | ||
From: tomat@lenderlabdot.com
Operating system: All
PHP version: 4.1.2
PHP Bug Type: Feature/Change Request
Bug description: array_recurse_safe()
Will solve problems like:
http://bugs.php.net/bug.php?id=16064
This code doesn't work, but I don't know enough C to write it right. It
would work if PHP had a method for determining if two variables are the
same reference. (unless there is one that I haven't found...)
<?php
$a=array(1,2,3);
$a[3]=&$a;
$a=array_recurse_safe($a);
array_merge_recursive($a,$a);
var_dump($a);
function array_recurse_safe($array, $parents=array()) {
$parents[]=&$array;
foreach($array as $element) {
if(is_array($element) || is_object($element)) {
$loop=false;
foreach($parents as $parent) {
if(&$parent == &$element) {
unset($element);
$loop=true;
}
}
if(!$loop) {
$element=array_recurse_safe($element, $parents);
}
}
}
return $array;
}
?>
Also, the usage of unset() is improper here, but I left it in for
simplicity.
--
Edit bug report at http://bugs.php.net/?id=17279&edit=1
--
Fixed in CVS: http://bugs.php.net/fix.php?id=17279&r=fixedcvs
Fixed in release: http://bugs.php.net/fix.php?id=17279&r=alreadyfixed
Need backtrace: http://bugs.php.net/fix.php?id=17279&r=needtrace
Try newer version: http://bugs.php.net/fix.php?id=17279&r=oldversion
Not developer issue: http://bugs.php.net/fix.php?id=17279&r=support
Expected behavior: http://bugs.php.net/fix.php?id=17279&r=notwrong
Not enough info: http://bugs.php.net/fix.php?id=17279&r=notenoughinfo
Submitted twice: http://bugs.php.net/fix.php?id=17279&r=submittedtwice
register_globals: http://bugs.php.net/fix.php?id=17279&r=globals