递归获取多对多节点关系

//连接节点表
$nodesObj = M("nodes");
$nextNodeIds = $nodesObj->where($map)->getField("id",true);//根据条件查询,获取初始水厂节点

//连接节点关系表
$nodeRelationObj = M("node_relation");

$relations = array();
$innerRelation = array();
foreach ($nextNodeIds as $nextNodeId) {
   $this->getNodeRelation($relations,$innerRelation,$nodeRelationObj,$nextNodeId);      
}
// 获取地区所有节点关系
    // $nodeRelationObj:node_relation表连接
    // $nextNodeIds: 初始化水源地Id
    public function getNodeRelation(&$relations,$innerRelation,$nodeRelationObj,$nextNodeId)
    {

            $currentNodeId = $nextNodeId;//获取当前水源地id

            array_push($innerRelation,$currentNodeId);
            $nextNodeIds = $nodeRelationObj->where("currentNode=%d",$currentNodeId)->getField("nextnode",true);//获取下个节点id

            if ($nextNodeIds == '' || $nextNodeIds == null) {
                array_push($relations,$innerRelation);
            }
            else {
                foreach ($nextNodeIds as $nextNodeId) {
                    $this->getNodeRelation($relations,$innerRelation,$nodeRelationObj,$nextNodeId);      
                }
            }
    }
结果

[[“1”,”2”,”3”,”4”],[“1”,”2”,”3”,”7”],[“1”,”2”,”5”,”4”],[“1”,”2”,”5”,”6”]] 
数据库:



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值