php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #17279 array_recurse_safe()
Submitted: 2002-05-16 18:09 UTC Modified: 2013-07-12 13:20 UTC
Votes:2
Avg. Score:3.5 ± 1.5
Reproduced:2 of 2 (100.0%)
Same Version:1 (50.0%)
Same OS:1 (50.0%)
From: tomat at lenderlabdot dot com Assigned:
Status: Wont fix Package: *General Issues
PHP Version: 4.1.2 OS: All
Private report: No CVE-ID:
Have you experienced this issue?
Rate the importance of this bug to you:

 [2002-05-16 18:09 UTC] tomat at lenderlabdot dot com
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.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-07-12 13:20 UTC] maarten@php.net
-Status: Open +Status: Wont fix -Package: Feature/Change Request +Package: *General Issues
 [2013-07-12 13:20 UTC] maarten@php.net
Related issue already fixed this problem.
 
PHP Copyright © 2001-2014 The PHP Group
All rights reserved.
Last updated: Fri Apr 25 07:02:14 2014 UTC