php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #63049 recursive_iterator_to_array for recursive iterators
Submitted: 2012-09-09 12:14 UTC Modified: 2018-08-23 01:04 UTC
Votes:3
Avg. Score:3.7 ± 0.9
Reproduced:2 of 2 (100.0%)
Same Version:1 (50.0%)
Same OS:1 (50.0%)
From: franssen dot roland at gmail dot com Assigned:
Status: Open Package: SPL related
PHP Version: 5.4.6 OS: Ubuntu
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: franssen dot roland at gmail dot com
New email:
PHP Version: OS:

 

 [2012-09-09 12:14 UTC] franssen dot roland at gmail dot com
Description:
------------
It would be nice to have a recursive_iterator_to_array function to transform a \RecursiveIterator to a full recursive array...


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-12-21 10:29 UTC] datibbaw@php.net
Why not just do this instead?

iterator_to_array(new RecursiveIteratorIterator($it));
 [2012-12-21 10:35 UTC] datibbaw@php.net
Ignore my previous comment. That would flatten the array instead. My apologies.
 [2012-12-21 13:23 UTC] franssen dot roland at gmail dot com
I currently have this... which also isn't restricted to just \RecursiveIterator(s) but uses a more generic approach.

<?php
function recursive_iterator_to_array(\Traversable $it)
{
    $result = array();
    foreach($result as $key => $value) {
        if($value instanceof \Traversable) {
            $result[$key] = recursive_iterator_to_array($value);
        } else {
            $result[$key] = $value;
        }
    }
    return $result;
}
 [2013-08-25 09:14 UTC] stanislav at nechutny dot net
TO: franssen dot roland

In your code is error. On line 5 it should be "foreach($it as $key => $value) {"
 [2018-08-23 01:04 UTC] carusogabriel@php.net
-Summary: iterator_to_array for recursive iterators +Summary: recursive_iterator_to_array for recursive iterators
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Nov 21 14:01:29 2024 UTC