在php中将数组作为树遍历

news/2024/7/5 3:22:31

问题:在php中将数组作为树遍历

我有一个描述层次结构的数据库表。这是结构

 id | pid | uid 
  1    5     2
  2    2     3
  3    2     4
  4    2     6
  5    3     7

在树形结构中,它看起来是这样的。这只是一个例子,它们可能是更多的节点。

         2 
      /  |  \
     3   4   6
    /      
   7 

因此,在 php 和 mysql 中,我获取所有数据并将其保存到数组中。

我想遍历该数组以确定例如特定级别中的 id 数量,我希望能够从一个级别检索所有节点。

我怎么能在php中做到这一点?

编辑

这就是我创建数组的方式:

foreach ( $resultSet as $row ) {
    $entry = array();
    $entry['id'] = $row->id;
    $entry['uid'] = $row->uid;
    $entry['rid'] = $row->rid;
    $entry['date'] = $row->date;
    $entries [] = $entry;
}

这就是我现在使用答案创建树的方式

$tree = array();
foreach($entries as $key){
   $this->adj_tree($tree, $key);
}
return $tree;

但是当我打印 $tree 时,我得到了一些奇怪的输出

Array ( [24] => Array ( [id] => 6 [uid] => 24 [rid] => 83 [date] => 2011-06-15
17:54:14 ) [] => Array ( [_children] => Array ( [0] => Array ( [id] => 6 [uid] => 24 
[rid] => 83 [date] => 2011-06-15 17:54:14 ) [1] => Array ( [id] => 6 [uid] => 24 [rid] =>
83 [date] => 2011-06-15 17:54:14 ) ) ) ) 

但实际上应该有一个 uid 为 24 的父母和两个孩子 82 和 83

解答

你没有说你是如何使用你的表的,但我猜它是用于商店类别树或类似的东西,即不需要任何复杂存储的小型数据集。您可以一次读取整个表格并使用 php 动态构建树结构。它是这样的

function adj_tree(&$tree, $item) {
    $i = $item['uid'];
    $p = $item['pid'];
    $tree[$i] = isset($tree[$i]) ? $item + $tree[$i] : $item;
    $tree[$p]['_children'][] = &$tree[$i];
}

例子:

$tree = array();
$rs = my_query("SELECT * FROM categories");
while($row = my_fetch($rs))
    adj_tree($tree, $row);

最后,您会得到一个项目数组,其中每个项目都包含 '_children' 子数组,而该子数组又包含对其他项目的引用(或为空)。

完整的示例,带有来自问题的数据

$entries = array(
  array('id' => 1, 'pid' => 5, 'uid' => 2),
  array('id' => 2, 'pid' => 2, 'uid' => 3),
  array('id' => 3, 'pid' => 2, 'uid' => 4),
  array('id' => 4, 'pid' => 2, 'uid' => 6),
  array('id' => 5, 'pid' => 3, 'uid' => 7),
);

$tree = array();
foreach($entries as $row)
    adj_tree($tree, $row);

function print_tree($node, $indent) {
    echo str_repeat('...', $indent) . $node['uid'], "<br>\n";
    if(isset($node['_children']))
        foreach($node['_children'] as $child)
            print_tree($child, $indent + 1);
}

print_tree($tree[2], 0);

http://lihuaxi.xjx100.cn/news/237148.html

相关文章

以太坊是什么,为什么这么火?

链客&#xff0c;专为开发者而生&#xff0c;有问必答&#xff01; 此文章来自区块链技术社区&#xff0c;未经允许拒绝转载。 以太坊是什么 以太坊&#xff08;Ethereum&#xff09;是一个建立在区块链技术之上&#xff0c; 去中心化应用平台。它允许任何人在平台中建立和使…

4.10日一直报错application未注入的问题解决

1.db.propertities 里面连接的是正式库,改为5522测试库 2.将pom.xml右键run as 后点击 instal转载于:https://www.cnblogs.com/CrisZjie180228/p/8793502.html

unix的发展

转载http://blog.51cto.com/1193432/1671058转载于:https://www.cnblogs.com/vwei/p/9588823.html

node学习笔记

1.node.js的回调函数的两个参数&#xff1a;第一个参数代表错误信息&#xff0c;第二个参数代表结果。 if (err) {// 出错了 } else {// 正常 } 复制代码注&#xff1a;当正常读取时&#xff0c;err参数为null&#xff0c;data参数为读取到的String。当读取发生错误时&#xff…

Web3与智能合约交互实战

链客&#xff0c;专为开发者而生&#xff0c;有问必答&#xff01; 此文章来自区块链技术社区&#xff0c;未经允许拒绝转载。 Web3与智能合约交互实战 以太坊中智能合约和web3交互实战 最近区块链、以太坊十分的火&#xff0c;所有就会有许多人去进入区块链这个工作&#x…

git 覆盖本地修改_Git拉力–如何使用Git覆盖本地更改

git 覆盖本地修改When you learn to code, sooner or later youll also learn about Version Control Systems. And while there are many competing tools in this space, one of them is the de facto standard used by almost everyone in the industry. Its so popular tha…

你和区块链的距离就差这篇文章!

链客&#xff0c;专为开发者而生&#xff0c;有问必答&#xff01; 此文章来自区块链技术社区&#xff0c;未经允许拒绝转载。 近年来&#xff0c;“区块链”逐渐成为热门话题&#xff0c;2018年各种关于区块链的行业资讯、投融资创业、技术和应用探索等集中爆发&#xff0c;…

JSP内置对象基础知识小结

JSP提供9大内置内象&#xff1a;一、request内象&#xff1a;封装了由客户端生成的HTTP请求的所有细节&#xff0c;主要包括了http头信息&#xff0c;系统信息&#xff0c;请求方式&#xff0c;请求参数等。1、获取访问请求参数&#xff1a;request.getParameter("arg&quo…