递归函数的返回结果

时间:2019-06-28 08:22:55

标签: php

我有一个递归函数,该函数当前MyModel requestID = new MyModel { "id" = "123" }; MyModel toUpdate = new MyModel { "is_cancelled" : true }; var builder = Builders<MyModel>.Update; UpdateDefinition<MyModel> update =null; toUpdate.GetType().GetProperties().ToList().ForEach( x => update = builder.Set(x.Name, x.GetValue(toUpdate, null)) FilterDefinition<MyModel> filter = requestID.ToBsonDocument(); if (update == null) return; collection.FindOneAndUpdate(filter, update); 个结果。我希望此函数返回结果并在标记内使用echo循环它们。

我知道我应该对每个数组使用某种迭代以获得我想要的结果,但是失败了。目前,我的代码如下所示(尝试进行迭代):

foreach

函数的传入数组:

public static function recursiveChildCategory($categories = array(), $depth = 0, $i = 0) {
    $ca = [];

    // Loop through categories
    foreach($categories as $key => $category){
        echo str_repeat("&nbsp;&nbsp;", $depth);
        echo "<a href='".implode('/', $category['breadcrumb'])."'>{$category['title']}</a>";
        echo '<br>';

       $ca[$i] = [
           'id' => $category['id'],
           'title' => $category['title'],
       ];

       if(isset($category['child'])) {
           // Loop
           self::recursiveChildCategory($category['child'], $depth + 1, $i++);
       }
   }
   return $ca;
}

当前,它仅返回第一级子类别“沙漠”,但未返回有关“空间”的任何信息。

作为理想结果,我希望函数返回所有带有Array ( [0] => Array ( [id] => 7 [title] => Deserts [slug] => deserts [child] => Array ( [0] => Array ( [id] => 8 [title] => Space [slug] => space [child] => Array ( [0] => Array ( [id] => [title] => [slug] => [child] => Array ( ) ) ) ) ) ) ) 的类别,并返回到多个子类别的无限路径(以与当前$depth所做的相同的工作)。

感谢您的咨询

1 个答案:

答案 0 :(得分:0)

尝试一下,告诉我您是否在寻找它:

用于测试的数组

$array = [
    0 => [
        "id" => 7,
        "title" => "Deserts",
        "slug" => "deserts",
        "child" => [
            0 => [
                "id" => 8,
                "title" => "Space",
                "slug" => "space",
                "child" => [
                    0 => [
                        "id" => 9,
                        "title" => "Test",
                        "slug" => "test"
                    ]
                ]
            ]
        ]
    ]    
];

递归函数

function recursiveChildCategory($categories, $depth = 0, $ca = []) {
    // Loop through categories
    foreach($categories as $key => $category){
       $ca[$depth] = [
           'id' => $category['id'],
           'title' => $category['title'],
       ];

       if(isset($category['child'])) {
           // Loop
           $ca = recursiveChildCategory($category['child'], $depth + 1, $ca);
       } else {
            break;
       }
   }
   return $ca;
}

现在,使用方法

$test = recursiveChildCategory($array);
var_dump($test);

这是输出

array(3) {
  [0]=>
  array(2) {
    ["id"]=>
    int(7)
    ["title"]=>
    string(7) "Deserts"
  }
  [1]=>
  array(2) {
    ["id"]=>
    int(8)
    ["title"]=>
    string(5) "Space"
  }
  [2]=>
  array(2) {
    ["id"]=>
    int(9)
    ["title"]=>
    string(4) "Test"
  }
}

这里是测试链接:http://sandbox.onlinephpfunctions.com/

编辑我进行了一些修改,因为在OP示例数组中,第一个“深度”可以具有多个结果,这是“新”解决方案:

要测试的数组:

$array = [ 
    "0" => 
        [ "id" => 3, "title" => "Subcat", "slug" => "subcat", "child" => 
            [ "0" => 
                [ "id" => 5, "title" => "Subsubcat2", "slug" => "subcat2", "child" => 
                    [ "0" =>
                        [ "id" => "", "title" => "", "slug" =>"", "breadcrumb" => [ "0" => "homeworld", "1" => "cat", "2" => "subcat", "3" => "subcat2" ], "child" => [ ] ] 
                    ]
                ] 
            ]
        ],

    "1" => 
        [ "id" => 4, "title" => "Kalahari", "slug" => "kalahari", "child" => 
            [ "0" => [ "id" => 7, "title" => "deserts", "slug" => "deserts", "child" =>
                [ "0" => 
                    [ "id" => 8, "title" => "Ural", "slug" => "ural", "child" => 
                        [ "0" => [ "id" =>"", "title" =>"", "slug" =>"", "child" =>  [ ] ] ]
                        ]
                    ]
                ]
            ]
        ]
    ];

功能:我只是添加$ca[$depth][]而不是$ca[$depth]

function recursiveChildCategory($categories, $depth = 0, $ca = []) {
    // Loop through categories
    foreach($categories as $key => $category){
       $ca[$depth][] = [
           'id' => $category['id'],
           'title' => $category['title'],
       ];

       if(isset($category['child'])) {
           // Loop
           $ca = recursiveChildCategory($category['child'], $depth + 1, $ca);
       } else {
            break;
       }
   }
   return $ca;
}

现在结果

$test = recursiveChildCategory($array);

foreach ($test as $depth => $c) {
    echo "depth : ".$depth."\n";
    foreach ($c as $result) {
        echo "Title : ".$result['title']."\n";
    }
     echo "=============\n";
}

输出是:

depth : 0
Title : Subcat
Title : Kalahari
=============
depth : 1
Title : Subsubcat2
Title : deserts
=============
depth : 2
Title : 
Title : Ural
=============
depth : 3
Title : 
=============

在这里测试:link

相关问题