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
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
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

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-2024 The PHP Group
All rights reserved.
Last updated: Thu Mar 28 19:01:29 2024 UTC