簡體   English   中英

樹形菜單數組的遞歸函數

[英]Recursive function for tree menu array

請我有2級菜單的功能...但是我不知道如何將其用於3級菜單。 你能幫我嗎 ? 示例數組

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

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

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

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

        [/c300112024-akvaristika-a-teraristika] => Array
            (
                [0] => /c300112025-krmiva
            )

    )

而且我的功能只能工作2級而不是3級

  public function saveCategoryTree($categoryTree, Eshop $eshop)
{

    $em = $this->getEntityManager();


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


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

        }

    }
    exit;
}

謝謝你的幫助。 對不起我的英語不好。

將您的子迭代邏輯移至新函數並遞歸調用;

這樣,它將適用於2、3、4,...層的樹功能。

編輯 :急於使用,我已經在PHP 7中構建了此代碼。請注意,這在PHP <7上將不起作用。可以隨意刪除類型提示並返回類型聲明。

我會嘗試以下方法:

    /**
     * @var EntityManager $_em
     */
    private $_em;
    /**
     * @var Eshop $_eshop
     */
    private $_eshop;

    /**
     * @param array $categoryTree
     * @param Eshop $eshop
     */
    public function saveCategoryTree(array $categoryTree, Eshop $eshop)
    {
        $this->_em    = $this->getEntityManager();
        $this->_eshop = $eshop;
        foreach ($categoryTree as $categoryKey => $categoryChildren)
        {
            if (is_array($categoryChildren))
            {
                $parentCategory = $this->_processCategory($categoryKey, $categoryChildren);
                $this->_processRecursiveChildren($parentCategory, $categoryChildren);
            } else
            {
                $this->_processCategory($categoryKey);
            }
        }
        exit;
    }

    /**
     * @param string        $categoryKey
     * @param Category|null $parentCategory
     *
     * @return Category
     */
    private function _processCategory(string $categoryKey, Category $parentCategory=null): Category
    {
        $category   = new Category();
        $parentName = explode("/", $categoryKey);
        $parentName = end($parentName);
        if ($parentCategory !== null)
        {
            $category->setParentCategory($parentCategory);
        }
        $category
            ->setCreatedAt(new \DateTime())
            ->setLastCheckAt(new \DateTime())
            ->setLastHttpStatusCode(\Symfony\Component\HttpFoundation\Response::HTTP_OK)
            ->setActive(true)
            ->setLeaf(false)
            ->setEshop($this->_eshop)
            ->setName($parentName)
            ->setLink($categoryKey)
        ;
        $this->_em->persist($category);
        $this->_em->flush();

        return $category;
    }

    /**
     * @param Category $parentCategory
     * @param array    $children
     */
    private function _processRecursiveChildren(Category $parentCategory, array $children)
    {
        foreach ($children as $childKey => $childItems)
        {
            if (is_array($childItems))
            {
                $nextParentCategory = $this->_processCategory($childKey, $parentCategory);
                $this->_processRecursiveChildren($nextParentCategory, $childItems);
            } else
            {
                $this->_processCategory($childItems, $parentCategory);
            }
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM