Recursive function for tree menu array
<p>please I have function for 2 level menu... but I dont know how use it for 3 level. Can you help me please ? Example array</p>

<pre><code>  [/c300112000-zvire] =&gt; Array
    (
        [/c300112001-pes] =&gt; Array
            (
                [0] =&gt; /c300112005-antiparazitni-pripravky
                [1] =&gt; /c300112002-granulovana-krmiva
                [2] =&gt; /c300112009-hracky-misky-a-ostatni
                [3] =&gt; /c300112003-konzervy-a-kapsicky
                [4] =&gt; /c300112004-pamlsky-a-doplnky-stravy
                [5] =&gt; /c300112008-psi-hygiena-a-zdravi
            )

        [/c300112010-kocka] =&gt; Array
            (
                [0] =&gt; /c300112015-antiparazitni-pripravky
                [1] =&gt; /c300112011-granulovana-krmiva
                [2] =&gt; /c300112017-hracky-a-misky
                [3] =&gt; /c300112016-kocici-hygiena-a-zdravi
                [4] =&gt; /c300112012-konzervy-a-kapsicky
                [5] =&gt; /c300112013-pamlsky-a-doplnky-stravy
                [6] =&gt; /c300112014-podestylky
            )

        [/c300112018-mala-zvirata] =&gt; Array
            (
                [0] =&gt; /c300112019-krmiva
                [1] =&gt; /c300112020-steliva
            )

        [/c300112021-ptactvo] =&gt; Array
            (
                [0] =&gt; /c300112022-krmiva
                [1] =&gt; /c300112023-steliva
            )

        [/c300112024-akvaristika-a-teraristika] =&gt; Array
            (
                [0] =&gt; /c300112025-krmiva
            )

    )
</code></pre>

<p>And my function working only 2 level not 3</p>

<pre><code>  public function saveCategoryTree($categoryTree, Eshop $eshop)
{

    $em = $this-&gt;getEntityManager();


    for ($i = 0; $i &lt; sizeof($categoryTree); $i++) {
        $categoryParent = new Category();
        $parent = array_keys($categoryTree)[$i];
        $parentName = explode("/", $parent);
        $parentName = end($parentName);
        $categoryParent-&gt;setCreatedAt(new \DateTime());
        $categoryParent-&gt;setLastCheckAt(new \DateTime());
        $categoryParent-&gt;setLastHttpStatusCode(\Symfony\Component\HttpFoundation\Response::HTTP_OK);
        $categoryParent-&gt;setActive(true);
        $categoryParent-&gt;setLeaf(false);
        $categoryParent-&gt;setEshop($eshop);
        $categoryParent-&gt;setName($parentName);
        $categoryParent-&gt;setLink($parent);
        $em-&gt;persist($categoryParent);
        $em-&gt;flush();


        foreach ($categoryTree[array_keys($categoryTree)[$i]] as $childItem) {
            if (is_array($childItem)) {
             echo "Is array ! Recursive call";
            } else {
                $child = new Category();
                $child-&gt;setParentCategory($categoryParent);
                $child-&gt;setCreatedAt(new \DateTime());
                $child-&gt;setLastCheckAt(new \DateTime());
                $child-&gt;setLastHttpStatusCode(\Symfony\Component\HttpFoundation\Response::HTTP_OK);
                $child-&gt;setActive(true);
                $child-&gt;setLeaf(true);
                $child-&gt;setEshop($eshop);
                $child-&gt;setName($childItem);
                $child-&gt;setLink($childItem);
                $em-&gt;persist($child);
                $em-&gt;flush();
            }

        }

    }
    exit;
}
</code></pre>

<p>Thanks for any help. Sorry for my english.</p>