一维整数数组与多维对象数组的比较

时间:2019-01-09 10:26:16

标签: php arrays

在我什至开始之前,我想为您可能容易遇到的问题道歉,因为我对此很陌生。因此,让我们直接说清楚。

我有一个巨大的多维数组(它是一棵类别树,每个类别在内部都有另一个类别,另一个在另一个类别中,依此类推)。整个数组实际上包含对象(每个键都是“ category”对象的实例),因此其中包含了所有内容(Id,parentId,children(array)等)。

我还有第二个数组,它是ID的简单数组(一维数组,仅包含数字-类别ID)。

现在,我正在尝试从树中的第二个数组中查找这几个ID,并获得(作为比较结果)树的每个分支中的最低ID。

对于一个非常简单的示例(对不起,很难解释):

树数组:

  • 1
    • 5
      • 7
      • 11
        • 12
    • 6
  • 2
  • 3
  • 4
    • 8
      • 13
    • 9
    • 10

在第二个数组中,是到我需要的类别的整个路线以及其他路线,例如:

[1] = 1,
[5] = 5,
[11] = 11,
[12] = 12,
[4] = 4,
[8] = 8

我只需要获取第二个数组中分支的最后一个类别,因此这里的结果应该是 12 8 ,因为它们是最后一个如果您明白我的意思,那么它们分支中的元素...不必一定是叶子。

谢谢您的任何想法!

//编辑:我忘了展示自己在尝试什么,我真的很抱歉,我们开始:

    private function buildRows($products): array {
    $export = [ ]; // final array ($row)
    foreach ( $products as $product ) {
        $producer = $this->producers [$product->getProducerId ()];
        foreach ( $product->getSpecies () as $specie ) {
            $data = [ 
                    'id' => $product->getId (),
                    'url_vyrobce' => $producer->getUrl ()
            ];
            $tempUrl  = $producer->getUrl();
            foreach ( $this->csvRegions as $headCode ) {
                foreach ( $product->getRegions () as $region ) {
                    $data [$region->getRegionCode () . '_name'] = $region->getName ();
                }
            }

            $data ['catalog_no'] = $product->getCatalogNo ();
            $data ['catalog_no_suffix'] = $specie->getSuffix ();

            $productCategories = $product->getCategories(); 
            $leaf = [];
            $leaf[] = $this->findCategoryLeaves($product->getCategories(), $this->categoryTree);
            dump($leaf);
            foreach ( $productCategories as $catId ) {
                //$ids[] = $this->findCategoriesRoot($catId, $this->categoryTree);
                $category = $this->categoryDao->getCategory ( $catId );
                $parentId = $category->getParentCategoryId();
                //$this->categoryDao->loadParents ( $category ); // get parrents for this needle
                /*foreach($this->categoryTree as $key => $cat){
                    if($cat['id'] === $parentId){
                        $parents[] = $key;
                    }
                    dump($parents[]);
                }*/
                /*      
                while ($parent = $category->getParentCategory()) {
                    if (in_array ( $parent->getId(), $productCategories )) {
                        array_push ( $parents, $parent->getId() );
                    }
                    if ($category->getParentCategory()) {
                        $category = $parent;
                    }
                }
                */
            }
            $row = [ ];
            foreach ( $this->createCsvHead () as $col ) {
                if (isset ( $data [$col] )) {
                    $row [] = $data [$col];
                } else {
                    $row [] = '';
                }
            }

            $export [] = $row; 
        }
    }
    return $export;
}

在这里,我只是为CSV文件做一些准备,最终将根据数据创建这些文件。我正在调用一个函数

$this->FindCategoryLeaves($product->getCategories, $this->categoryTree)

此功能在下面(尚未完成,因为我坚持使用该解决方案。我想它应该是递归的,但我不能使它起作用)

private function findCategoryLeaves($productCategory, $categoryTree){
    dump($categoryTree);
    $leaves = [];
    $branches = [];
    dump($productCategory);
    $lastElement = end($productCategory);
    foreach($productCategory as $productCatId){
        foreach($categoryTree as $catTree){
            $catTreeId = $catTree->getId();
            if($catTreeId === $productCatId){
                if($productCatId === $lastElement){ 
                    $branches[] = $productCatId; 
                    dump($catTreeId . ' | LAST ID');
                    findCategoryLeaves($productCategory, $categoryTree); 
                }
                //}else{
                //  $leaves[] = $productCatId;
            }
            dump($catTreeId .' | ID NOT FOUND!');
            /*$subs = $catTree->getSubcategories();
            if(array_key_exists($productCategoryId, $subs)){
                dump($subs);
                $leaf[] = $productCategoryId;
                $this->findCategoryLeaves($productCategoryId, $subs);
            }else{
                dump($leaf);
                return $leaf;
            }*/
        }
        }
    }

0 个答案:

没有答案