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: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: tomat at lenderlabdot dot com
New email:
PHP Version: OS:

 

 [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

Pull Requests

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-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 15:01:29 2024 UTC