Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/60.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 MySQL显示特定子id的父项_Php_Mysql_Tree - Fatal编程技术网

使用PHP MySQL显示特定子id的父项

使用PHP MySQL显示特定子id的父项,php,mysql,tree,Php,Mysql,Tree,我试图显示链数据中特定子级的所有父级,如下图所示 对于此表数据 到目前为止,我已经尝试了以下方法: // for this one i am getting a indefinite loop public function buildBinaryTree($user_id) { $pictures = []; $p_info = $this->memberRepository->getMemberInfo($user_id); $p_id = $p_

我试图显示链数据中特定子级的所有父级,如下图所示

对于此表数据

到目前为止,我已经尝试了以下方法:

// for this one i am getting a indefinite loop

public function buildBinaryTree($user_id)
{
    $pictures = [];

    $p_info = $this->memberRepository->getMemberInfo($user_id);
    $p_id = $p_info->member_id;

    if(empty($p_id)){
        return;
    }

    $children = $this->buildBinaryTree($p_id);

    if ($children) {
        echo "child ". $p_id."</br>";
    }
}

public function buildTree($all_members, $user_id)
{
    $pictures = [];

    foreach ($all_members as $item) {

        if ($item->member_id == $user_id) {

            $pictures[] = [
                'member_id'      => $item->member_id,
                'referrar_id'     => $item->user_id
            ];

            $children = $this->buildTree($all_members, $item->user_id);

            if ($children) {
                $item['children'] = $children;
            }            
        }
    }

    return $pictures;

}

我正在寻找使用PHP MySQL在数据链中查找特定子id的所有顶级父级的最佳方法?

分层数据可能很棘手,尤其是如果您不能保证它的深度(例如)永远不会超过2级或类似的深度

前面的回答可能会有所帮助:


您可以在上面链接中的一些嵌套集合中找到这一点,但我有幸使用一个新的方法用更复杂的层次结构重新构造了数据模型。它确实需要重新构造数据,但对于像您这样的情况,它非常有助于构建层次树的完整表示形式,而且性能非常好。

您需要尽量减少传递次数,通过执行递归函数,您可能会在以后遇到严重的性能问题。通过单次传递并使用引用,您可以让它运行得非常快,工作得非常好。内存使用量也将略高于原始数据集,因为所有操作都是通过引用而不是复制完成的。如果更新其中一条记录,则所有记录都会随之更新

输入阵列

变量,您将在其中存储引用

循环记录集

在这里检查您是否添加了此行,理论上,除非您有重复的条目,否则这应该始终为真

检查是否有父项,然后检查父项是否已添加和/或其子项是否已初始化

将孩子的引用添加到家长上,特别注意&这表示它是通过引用

$tree变为以下内容


谢谢你详细的回答。对于作为输入数组“$rows”的大量数据,会发生什么情况。我需要获得上面屏幕截图中提到的特定儿童的所有顶级数据。我正在考虑为每个特定id获取父id,并将其保持到父id为NULL为止。你认为你能为我的特殊需要提供可行的样品吗?我将非常感谢你的帮助和支持
a[0]{
   'member_id' => 8,
   'user_id' => 7
},
a[1]{
   'member_id' => 7,
   'user_id' => 6
},
$rows = [
    [ 'id' => 1, 'parent' => null ],
    [ 'id' => 2, 'parent' => 1 ],
    [ 'id' => 3, 'parent' => 1 ],
    [ 'id' => 4, 'parent' => 2 ],
    [ 'id' => 5, 'parent' => 3 ],
    [ 'id' => 6, 'parent' => 4 ],
    [ 'id' => 7, 'parent' => 5 ],
    [ 'id' => 8, 'parent' => 6 ],
    [ 'id' => 9, 'parent' => 7 ],
    [ 'id' => 10, 'parent' => 8 ],
];
$tree = [];
foreach($rows as $row) {
    if (
        false === array_key_exists($row['id'], $tree) ||
        false === array_key_exists('data', $tree[$row['id']])

    ) {
        $tree[$row['id']]['data'] = $row;
    }
    if (
        $row['parent'] &&
        (
            false === array_key_exists($row['parent'], $tree) ||
            false === array_key_exists('children', $tree[$row['parent']])
        )
    ) {
        $tree[$row['parent']]['children'] = [];
    }
    if ($row['parent']) {
        $tree[$row['parent']]['children'][] = &$tree[$row['id']];
    }
}
[
  1 =>
  [
    'data' =>
    [
      'id' => 1,
      'parent' => NULL,
    ],
    'children' =>
    [
      0 =>
      [
        'data' =>
        [
          'id' => 2,
          'parent' => 1,
        ],
        'children' =>
        [
          0 =>
          [
            'data' =>
            [
              'id' => 4,
              'parent' => 2,
            ],
            'children' =>
            [
              0 =>
              [
                'data' =>
                [
                  'id' => 6,
                  'parent' => 4,
                ],
                'children' =>
                [
                  0 =>
                  [
                    'data' =>
                    [
                      'id' => 8,
                      'parent' => 6,
                    ],
                    'children' =>
                    [
                      0 =>
                      [
                        'data' =>
                        [
                          'id' => 10,
                          'parent' => 8,
                        ],
                      ],
                    ],
                  ],
                ],
              ],
            ],
          ],
        ],
      ],
      1 =>
      [
        'data' =>
        [
          'id' => 3,
          'parent' => 1,
        ],
        'children' =>
        [
          0 =>
          [
            'data' =>
            [
              'id' => 5,
              'parent' => 3,
            ],
            'children' =>
            [
              0 =>
              [
                'data' =>
                [
                  'id' => 7,
                  'parent' => 5,
                ],
                'children' =>
                [
                  0 =>
                  [
                    'data' =>
                    [
                      'id' => 9,
                      'parent' => 7,
                    ],
                  ],
                ],
              ],
            ],
          ],
        ],
        'name' => 'test',
      ],
    ],
  ],
  2 =>
  [
    'data' =>
    [
      'id' => 2,
      'parent' => 1,
    ],
    'children' =>
    [
      0 =>
      [
        'data' =>
        [
          'id' => 4,
          'parent' => 2,
        ],
        'children' =>
        [
          0 =>
          [
            'data' =>
            [
              'id' => 6,
              'parent' => 4,
            ],
            'children' =>
            [
              0 =>
              [
                'data' =>
                [
                  'id' => 8,
                  'parent' => 6,
                ],
                'children' =>
                [
                  0 =>
                  [
                    'data' =>
                    [
                      'id' => 10,
                      'parent' => 8,
                    ],
                  ],
                ],
              ],
            ],
          ],
        ],
      ],
    ],
  ],
  3 =>
  [
    'data' =>
    [
      'id' => 3,
      'parent' => 1,
    ],
    'children' =>
    [
      0 =>
      [
        'data' =>
        [
          'id' => 5,
          'parent' => 3,
        ],
        'children' =>
        [
          0 =>
          [
            'data' =>
            [
              'id' => 7,
              'parent' => 5,
            ],
            'children' =>
            [
              0 =>
              [
                'data' =>
                [
                  'id' => 9,
                  'parent' => 7,
                ],
              ],
            ],
          ],
        ],
      ],
    ],
    'name' => 'test',
  ],
  4 =>
  [
    'data' =>
    [
      'id' => 4,
      'parent' => 2,
    ],
    'children' =>
    [
      0 =>
      [
        'data' =>
        [
          'id' => 6,
          'parent' => 4,
        ],
        'children' =>
        [
          0 =>
          [
            'data' =>
            [
              'id' => 8,
              'parent' => 6,
            ],
            'children' =>
            [
              0 =>
              [
                'data' =>
                [
                  'id' => 10,
                  'parent' => 8,
                ],
              ],
            ],
          ],
        ],
      ],
    ],
  ],
  5 =>
  [
    'data' =>
    [
      'id' => 5,
      'parent' => 3,
    ],
    'children' =>
    [
      0 =>
      [
        'data' =>
        [
          'id' => 7,
          'parent' => 5,
        ],
        'children' =>
        [
          0 =>
          [
            'data' =>
            [
              'id' => 9,
              'parent' => 7,
            ],
          ],
        ],
      ],
    ],
  ],
  6 =>
  [
    'data' =>
    [
      'id' => 6,
      'parent' => 4,
    ],
    'children' =>
    [
      0 =>
      [
        'data' =>
        [
          'id' => 8,
          'parent' => 6,
        ],
        'children' =>
        [
          0 =>
          [
            'data' =>
            [
              'id' => 10,
              'parent' => 8,
            ],
          ],
        ],
      ],
    ],
  ],
  7 =>
  [
    'data' =>
    [
      'id' => 7,
      'parent' => 5,
    ],
    'children' =>
    [
      0 =>
      [
        'data' =>
        [
          'id' => 9,
          'parent' => 7,
        ],
      ],
    ],
  ],
  8 =>
  [
    'data' =>
    [
      'id' => 8,
      'parent' => 6,
    ],
    'children' =>
    [
      0 =>
      [
        'data' =>
        [
          'id' => 10,
          'parent' => 8,
        ],
      ],
    ],
  ],
  9 =>
  [
    'data' =>
    [
      'id' => 9,
      'parent' => 7,
    ],
  ],
  10 =>
  [
    'data' =>
    [
      'id' => 10,
      'parent' => 8,
    ],
  ],
]