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:2
Avg. Score:3.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.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
Have you experienced this issue?
Rate the importance of this bug to you:

 [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

Add a Patch

Pull Requests

Add a Pull Request

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-2019 The PHP Group
All rights reserved.
Last updated: Fri Dec 06 10:01:25 2019 UTC