【问题标题】:Having trouble with a recursion algo递归算法有问题
【发布时间】:2012-12-25 05:14:30
【问题描述】:

好的,这是我的递归算法:

public function getCategoryTree($tree,$return = array()) {
    foreach ($tree->children as $child) {
      if (count($child->children) > 0 )
        $return[$tree->name] = $this->getCategoryTree($child, $return);
      else
        $return[] = $child->name;
    } 
    return $return;
  }

这是我试图遍历的数据结构的 sn-p

Object(stdClass)#290 (6) {
      ["category_id"]=>
      int(1)
      ["parent_id"]=>
      int(0)
      ["name"]=>
      string(4) "Root"
      ["position"]=>
      int(0)
      ["level"]=>
      int(0)
      ["children"]=>
      array(2) {
        [0]=>
        object(stdClass)#571 (7) {
          ["category_id"]=>
          int(2)
          ["parent_id"]=>
          int(1)
          ["name"]=>
          string(18) "Root MySite.com"
          ["is_active"]=>
          int(0)
          ["position"]=>
          int(0)
          ["level"]=>
          int(1)
          ["children"]=>
          array(11) {
            [0]=>
            object(stdClass)#570 (7) {
              ["category_id"]=>
              int(15)
              ["parent_id"]=>
              int(2)
              ["name"]=>
              string(9) "Widgets"
              ["is_active"]=>
              int(1)
              ["position"]=>
              int(68)
              ["level"]=>
              int(2)
              ["children"]=>
              array(19) {
                [0]=>
                object(stdClass)#566 (7) {
                  ["category_id"]=>
                  int(24)
                  ["parent_id"]=>
                  int(15)
                  ["name"]=>
                  string(16) "Blue widgets"
                  ["is_active"]=>
                  int(1)
                  ["position"]=>
                  int(68)
                  ["level"]=>
                  int(3)
                  ["children"]=>
                  array(0) {
                  }
                }

<snip....>

我正在尝试获取这样的 php 数据结构

categories = array( "Root" =>
                  array("Root MySite.com" =>
                    array( "Widgets" =>
                         // final element is NOT an array
                         array ("Blue Widgets", "Purple Widgets" ...) 
                    )
                  )
              )

我似乎无法使用我的递归算法获得我正在寻找的数据结构。任何帮助 会很棒。

最终我需要在前端再次解析并显示它,但另一天的另一个问题......

【问题讨论】:

  • 什么时候使用algo变得“酷”了?
  • 过去 2 年的某个时候,“史诗”这个词开始流行
  • 我不清楚“我正试图将它存储在 Mongo DB 中”。所以你需要一种将你的对象转换为字符串的方法???对象在序列化时可以保存为字符串。 php中有很多方法可以做到这一点。最重要的两个是:json-encodeserialize
  • luis 我为清楚起见编辑过,mongo db 部分无关紧要,只需要正确的 php 数据结构

标签: php data-structures recursion


【解决方案1】:

查看phpFiddle 以获得完整的工作示例。我发现的唯一错误是$this-&gt;getCategoryTree,它给了我一个致命错误 Using $this when not in object context。那么你确定这个函数在正确的范围内吗?

更新

我希望这个有效。 :)

function traverse($root, $return = array()) {
    $return[$root->name] = array();
    foreach ($root->children as $child) {
        if (count($child->children) > 0) {
            traverse($child, &$return[$root->name]);
        }else {
            array_push(&$return[$root->name], $child->name);
        }
    }
    return $return;
}

由此产生的输出是:

Array ( [Root] => 
    Array ( 
        [Root MySite.com] => 
            Array ( 
                [Widgets] => Array ( [0] => Blue Widget [1] => Purple Widget) 
                [Gizmos] => Array ( [0] => Blue Gizmos [1] => Purple Gizmos) 
            ) 
        [FooBar.com] => 
            Array ( 
                [Widgets] => Array ( [0] => Blue Widget [1] => Purple Widget) 
                [Gizmos] => Array ( [0] => Blue Gizmos [1] => Purple Gizmos) 
            ) 
    ) 
)

再说一遍,full working example

【讨论】:

  • +1 为您的工作小提琴,但array_push[] 相同,所以问题又是@bonez,出了什么问题?
  • 谢谢。终于明白你的意思了。是的,我在使用他的功能时发现的唯一错误是在上下文之外使用$this。我已经编辑了我的答案,现在它更像是一个评论,但我会顺其自然。
  • phpfiddle.org/main/code/nf6-nby 问题是这不适用于多个级别 1 类别,第一组将被覆盖。我之前会提到这一点,但我才意识到这个问题。请参阅 phpfiddle 以获取示例。谢谢大家。
  • 我已经更新了答案(链接和代码)。它现在产生这个Array ( [Root] =&gt; Array ( [0] =&gt; Array ( [Root MySite.com] =&gt; Array ( [0] =&gt; Array ( [0] =&gt; Blue Widget [1] =&gt; Purple Widget ) ) ) [1] =&gt; Array ( [FooBar.com] =&gt; Array ( [0] =&gt; Array ( [0] =&gt; Blue Gizmos [1] =&gt; Purple Gizmos ) ) ) ) )
  • 在这个例子中,你丢失了键名'Widgets'和'Gizmos' ..这就是为什么我一直在搞砸这个,看不出它是正确的: ) ... FooBar.com 也应该是 Root Mysite[1] 数组的键名,不是吗?
猜你喜欢
  • 1970-01-01
  • 2021-10-14
  • 1970-01-01
  • 2020-10-16
  • 2017-09-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多