PHP: Return an array from recursive function
<p>I have an array like this one:</p>

<pre><code>SimpleXMLElement Object
(
    [BrowseNodes] =&gt; SimpleXMLElement Object
        (
            [BrowseNode] =&gt; SimpleXMLElement Object
                (
                    [BrowseNodeId] =&gt; 969391031
                    [Name] =&gt; Bambine e ragazze
                    [Children] =&gt; SimpleXMLElement Object
                        (
                            [BrowseNode] =&gt; Array
                                (
                                    [0] =&gt; SimpleXMLElement Object
                                        (
                                            [BrowseNodeId] =&gt; 969394031
                                            [Name] =&gt; Calze
                                        )

                                    [1] =&gt; SimpleXMLElement Object
                                        (
                                            [BrowseNodeId] =&gt; 3635837031
                                            [Name] =&gt; Felpe
                                        )

                                    [2] =&gt; SimpleXMLElement Object
                                        (
                                            [BrowseNodeId] =&gt; 3635838031
                                            [Name] =&gt; Giacche
                                        )

                                    [3] =&gt; SimpleXMLElement Object
                                        (
                                            [BrowseNodeId] =&gt; 3635839031
                                            [Name] =&gt; Guanti da giocatore
                                        )

                                    [4] =&gt; SimpleXMLElement Object
                                        (
                                            [BrowseNodeId] =&gt; 969392031
                                            [Name] =&gt; Maglie
                                        )

                                    [5] =&gt; SimpleXMLElement Object
                                        (
                                            [BrowseNodeId] =&gt; 4351854031
                                            [Name] =&gt; Maglie per tifosi
                                        )

                                    [6] =&gt; SimpleXMLElement Object
                                        (
                                            [BrowseNodeId] =&gt; 3635840031
                                            [Name] =&gt; Magliette da portiere
                                        )

                                    [7] =&gt; SimpleXMLElement Object
                                        (
                                            [BrowseNodeId] =&gt; 969393031
                                            [Name] =&gt; Pantaloncini
                                        )

                                    [8] =&gt; SimpleXMLElement Object
                                        (
                                            [BrowseNodeId] =&gt; 3635841031
                                            [Name] =&gt; Pantaloncini da portiere
                                        )

                                    [9] =&gt; SimpleXMLElement Object
                                        (
                                            [BrowseNodeId] =&gt; 3635842031
                                            [Name] =&gt; Pantaloni
                                        )

                                    [10] =&gt; SimpleXMLElement Object
                                        (
                                            [BrowseNodeId] =&gt; 3635843031
                                            [Name] =&gt; Tute da ginnastica
                                        )

                                )

                        )

                    [Ancestors] =&gt; SimpleXMLElement Object
                        (
                            [BrowseNode] =&gt; SimpleXMLElement Object
                                (
                                    [BrowseNodeId] =&gt; 969386031
                                    [Name] =&gt; Abbigliamento
                                    [Ancestors] =&gt; SimpleXMLElement Object
                                        (
                                            [BrowseNode] =&gt; SimpleXMLElement Object
                                                (
                                                    [BrowseNodeId] =&gt; 937258031
                                                    [Name] =&gt; Calcio
                                                    [Ancestors] =&gt; SimpleXMLElement Object
                                                        (
                                                            [BrowseNode] =&gt; SimpleXMLElement Object
                                                                (
                                                                    [BrowseNodeId] =&gt; 524013031
                                                                    [Name] =&gt; Categorie
                                                                    [IsCategoryRoot] =&gt; 1
                                                                    [Ancestors] =&gt; SimpleXMLElement Object
                                                                        (
                                                                            [BrowseNode] =&gt; SimpleXMLElement Object
                                                                                (
                                                                                    [BrowseNodeId] =&gt; 524012031
                                                                                    [Name] =&gt; Sport e tempo libero
                                                                                )

                                                                        )

                                                                )

                                                        )

                                                )

                                        )

                                )

                        )

                )

        )

)
</code></pre>

<p>What I need to do is to build a breadcrumb using Anchestors. The one that is at the end of the list should be the first one. So, as an example:</p>

<p>Sport e tempo libero > Categorie > calcio...</p>

<p>I'm trying to iterate the xml with a function in this way without success:</p>

<pre><code>    $rec=$result-&gt;BrowseNodes-&gt;BrowseNode-&gt;Ancestors-&gt;BrowseNode;

    $bread=array();
    function recursive($r)
    {
        do{
            $bread[]=$r-&gt;BrowseNodeId;
            recursive($r-&gt;Ancestors-&gt;BrowseNode);
        }while(isset($r-&gt;Ancestors));
        $bread=array_reverse($bread);
        return $bread;
    }

    print_r(recursive($rec));
</code></pre>

<p>I found something similar on stackoverflow but no suggestions helped me to sort this out.</p>