Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 帮助使用递归函数从一个db查询创建父/子树_Php_Loops_Hierarchical Data - Fatal编程技术网

Php 帮助使用递归函数从一个db查询创建父/子树

Php 帮助使用递归函数从一个db查询创建父/子树,php,loops,hierarchical-data,Php,Loops,Hierarchical Data,一段时间以来,我一直在研究一个函数,从一个db查询生成一个多维数组,其中包含无限的父/子关系。我非常接近完成这项工作,但我仍然有一些问题 我将复制下面的代码并对其进行注释,以显示问题。如果可能的话,我希望这里有人能帮我 function build_child($data, $parent) { $roles = array(); foreach($data as $tkey => $tval) { if($data[$tkey]->parent_

一段时间以来,我一直在研究一个函数,从一个db查询生成一个多维数组,其中包含无限的父/子关系。我非常接近完成这项工作,但我仍然有一些问题

我将复制下面的代码并对其进行注释,以显示问题。如果可能的话,我希望这里有人能帮我

    function build_child($data, $parent) {

    $roles = array();

    foreach($data as $tkey => $tval) {
    if($data[$tkey]->parent_id==$parent) {

    $role = array();

    $role['role_id'] = $data[$tkey]->role_id;
    $role['role_name'] = $data[$tkey]->role_name;

    $children = build_child($data, $data[$tkey]->role_id);

    if( !empty($children) ) {

    $role['children'] = $children;

    }
    $roles[] = $role;

    return $roles;
    }

    }
    }
db查询生成的My array包含类似以下内容的数据:

Array
(
    [0] => stdClass Object
        (
            [role_id] => 1
            [role_name] => tester
            [parent_id] => 0
        )

)
然后我将这个数组传递给下面的函数来创建树

function create_role_tree($data) {

$roles = array();

foreach ($data as $tkey => $tval) {

    $role = array();

    $role['role_id'] = $data[$tkey]->role_id;
    $role['role_name'] = $data[$tkey]->role_name;

    $children = build_child($data, $data[$tkey]->role_id);

    if( !empty($children) ) {

    $role['children'] = $children;

    }

    $roles[] = $role;

    }

    return $roles;

    }
function create_role_tree($data) {
  $roles = array();

  foreach ($data as $tkey => $tval) {

    // Skip element, if it is a child element
    if ($data[$tkey]->parent_id != 0) {
      // Skip to next element
      continue;
    }

    // Else, go on as before...

  }
}
我添加了另一个如下所示的函数,因为我遇到了创建无限循环的问题。但如果可能的话,我最终希望从一个函数生成这个数组

    function build_child($data, $parent) {

    $roles = array();

    foreach($data as $tkey => $tval) {
    if($data[$tkey]->parent_id==$parent) {

    $role = array();

    $role['role_id'] = $data[$tkey]->role_id;
    $role['role_name'] = $data[$tkey]->role_name;

    $children = build_child($data, $data[$tkey]->role_id);

    if( !empty($children) ) {

    $role['children'] = $children;

    }
    $roles[] = $role;

    return $roles;
    }

    }
    }
下面是运行上述两个函数后剩下的数组。您将看到它几乎产生了正确的结果,但是我遇到的问题是,如果一个角色是另一个角色的子角色,它仍然显示在主数组中,我希望阻止这种情况发生。有谁能帮我阻止这种事情发生吗

Array
(
    [0] => Array
        (
            [role_id] => 1
            [role_name] => tester
            [children] => Array
                (
                    [0] => Array
                        (
                            [role_id] => 4
                            [role_name] => test 2
                        )

                )

        )

    [1] => Array
        (
            [role_id] => 4
            [role_name] => test 2
        )

    [2] => Array
        (
            [role_id] => 5
            [role_name] => test 3
        )

    [3] => Array
        (
            [role_id] => 6
            [role_name] => uyuiy
        )

    [4] => Array
        (
            [role_id] => 7
            [role_name] => uyuiy
            [children] => Array
                (
                    [0] => Array
                        (
                            [role_id] => 10
                            [role_name] => bamm
                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [role_id] => 11
                                            [role_name] => testing tree
                                        )

                                )

                        )

                )

        )

    [5] => Array
        (
            [role_id] => 8
            [role_name] => uyuiy
        )

    [6] => Array
        (
            [role_id] => 9
            [role_name] => test new
        )

    [7] => Array
        (
            [role_id] => 10
            [role_name] => bamm
            [children] => Array
                (
                    [0] => Array
                        (
                            [role_id] => 11
                            [role_name] => testing tree
                        )

                )

        )

    [8] => Array
        (
            [role_id] => 11
            [role_name] => testing tree
        )

)

感谢您查看

您只需跳过不属于主数组的元素,即具有
父\u id!=0
,它们是应该出现在树中其他位置的子元素

function create_role_tree($data) {

$roles = array();

foreach ($data as $tkey => $tval) {

    $role = array();

    $role['role_id'] = $data[$tkey]->role_id;
    $role['role_name'] = $data[$tkey]->role_name;

    $children = build_child($data, $data[$tkey]->role_id);

    if( !empty($children) ) {

    $role['children'] = $children;

    }

    $roles[] = $role;

    }

    return $roles;

    }
function create_role_tree($data) {
  $roles = array();

  foreach ($data as $tkey => $tval) {

    // Skip element, if it is a child element
    if ($data[$tkey]->parent_id != 0) {
      // Skip to next element
      continue;
    }

    // Else, go on as before...

  }
}