php中异步树中的递归函数与“;致命错误:允许的内存大小为134217728“;


Recursive function in async tree in php gets with "Fatal error: Allowed memory size of 134217728"

我曾尝试创建异步树,但当我调用函数内部的函数时,她给我带来了内存错误。我读过很多关于如何更改php内存限制以运行它的文章,我尝试过,但没有结果。但我真正想要的是一种"停止"函数的方法,而不是无限次地调用它。有人能帮我吗?

function treeview()
{    
    $i = 0;
    $result = array();
    $rs = mysql_query("select * from dados3 where nivel = 1");
    while($row = mysql_fetch_array($rs))
    {
        $node = array();
        $node['id'] = $row['id'];
        $node['text'] = $row['descricao'];
        $node['state'] = 'closed';
        $node['children'] = has_child($row['id']);
        array_push($result, $node);
    }  
echo json_encode($result);
}
function has_child($id){
    $result2 = array();
    $node_2 = array();
    $rs = mysql_query("select * from dados3 where id_nivel_superior=$id");
    $row = mysql_fetch_array($rs);
    $node_2['text'] = $row['descricao'];
    $node_2['state'] = 'closed';
    $node_2['children'] = has_child($id);
    array_push($result2, $node_2);
    return $result2;
}

我尝试了上面的方法,这会返回给我所有的子项为null,并且在级别3重复级别1。你认为这可能是错的吗?

function treeview()
{    
    $i = 0;
    $result = array();
    $rs = mysql_query("select * from dados3 where nivel = 1");
    while($row = mysql_fetch_array($rs))
    {
        $node = array();
        $node['id'] = $row['id'];
        $node['text'] = $row['descricao'];
        $node['state'] = 'closed';
        $node['children'] = has_child($row['id']);
        array_push($result, $node);
    }  
echo json_encode($result);
}
function has_child($id){
    $result2 = array();
    $node_2 = array();
    $rs = mysql_query("select * from dados3 where id_nivel_superior=$id");
    while($row = mysql_fetch_array($rs))
    {
        $node_2['text'] = $row['descricao'];
        $node_2['state'] = 'closed';
        $node_2['children'] = has_child($row['id']);
        array_push($result2, $node_2);
        return $result2;
    }
}

在图片中,您可以看到数据库结构和数据:

数据库的图像

我已经把return从循环中放了出来,但返回给我的json中的cidren都为null。在第4级(我想展示一些东西),它再次重复所有级别。

[{"id":"1","text":"1 - Cat.","state":"closed","children":[{"text":"1.1 - Sub.","state":"closed","children":[]},{"text":"1.2 - Sub.","state":"closed","children":[]},{"text":"1.3 - Sub.","state":"closed","children":[]}]},{"id":"2","text":"2 - Cat.","state":"closed","children":[{"text":"2.1 - Sub.","state":"closed","children":[{"text":"2.1.1 - Sub_sub","state":"closed","children":[{"text":"Faq 1 - categoria 1","state":"closed","children":[]}]}]},{"text":"2.2 - Sub.","state":"closed","children":[]},{"text":"2.3 - Sub.","state":"closed","children":[]}]},{"id":"4","text":"3- Cat.","state":"closed","children":[{"text":"3.1 - Sub.","state":"closed","children":[]},{"text":"3.2 - Sub.","state":"closed","children":[]},{"text":"3.3 - Sub.","state":"closed","children":[]}]},{"id":"5","text":"4 - Cat.","state":"closed","children":[{"text":"4.1 - Sub.","state":"closed","children":[]},{"text":"4.2 - Sub.","state":"closed","children":[]},{"text":"4.3 - Sub.","state":"closed","children":[]}]}]

如果你能帮我理解为什么孩子们没有出现,我将不胜感激。

致以最良好的问候。

问题似乎是对has_child()的递归调用正在传递$id,即最初传递给函数的参数。因此,发生的情况是,因为每次都用相同的参数调用函数,它将无休止地递归调用自己。

我怀疑线路

$node_2['children'] = has_child($id);

应该实际读取

$node_2['children'] = has_child($row['id']);

此外,我怀疑您可能也希望在has_child()中使用循环,就像在treeview()中一样,否则您将只在树的每个级别获得第一个项。

编辑

事实上,对不起,我可能误解了这里发生的事情。早些时候,我假设"id_nivel_superior"是父项的id,但我说这实际上是树中父项的级别,这是对的吗?如果是,是否有包含父项ID的内容,因为这是has_child()中SQL语句需要使用的列。如果您可以发布您的表结构和数据样本,这可能会有所帮助。

此外,has_child()中的return语句需要在循环之外。

好吧,为了回答您最近的编辑,我可能在这里遗漏了一些内容,但根据您所拥有的数据,您发布的JSON输出似乎是正确的。在一行上读取有点棘手,但如果您在http://jsonformatter.curiousconcept.com/你会看到它的样子:

[
   {
      "id":"1",
      "text":"1 - Cat.",
      "state":"closed",
      "children":[
         {
            "text":"1.1 - Sub.",
            "state":"closed",
            "children":[
            ]
         },
         {
            "text":"1.2 - Sub.",
            "state":"closed",
            "children":[
            ]
         },
         {
            "text":"1.3 - Sub.",
            "state":"closed",
            "children":[
            ]
         }
      ]
   },
   {
      "id":"2",
      "text":"2 - Cat.",
      "state":"closed",
      "children":[
         {
            "text":"2.1 - Sub.",
            "state":"closed",
            "children":[
               {
                  "text":"2.1.1 - Sub_sub",
                  "state":"closed",
                  "children":[
                     {
                        "text":"Faq 1 - categoria 1",
                        "state":"closed",
                        "children":[
                        ]
                     }
                  ]
               }
            ]
         },
         {
            "text":"2.2 - Sub.",
            "state":"closed",
            "children":[
            ]
         },
         {
            "text":"2.3 - Sub.",
            "state":"closed",
            "children":[
            ]
         }
      ]
   },
   {
      "id":"4",
      "text":"3- Cat.",
      "state":"closed",
      "children":[
         {
            "text":"3.1 - Sub.",
            "state":"closed",
            "children":[
            ]
         },
         {
            "text":"3.2 - Sub.",
            "state":"closed",
            "children":[
            ]
         },
         {
            "text":"3.3 - Sub.",
            "state":"closed",
            "children":[
            ]
         }
      ]
   },
   {
      "id":"5",
      "text":"4 - Cat.",
      "state":"closed",
      "children":[
         {
            "text":"4.1 - Sub.",
            "state":"closed",
            "children":[
            ]
         },
         {
            "text":"4.2 - Sub.",
            "state":"closed",
            "children":[
            ]
         },
         {
            "text":"4.3 - Sub.",
            "state":"closed",
            "children":[
            ]
         }
      ]
   }
]

据我所知,它与数据库相匹配。如果有什么不对劲的地方,你能告诉我你希望得到什么输出吗?

相关文章: