php树结构数组,PHP数组树结构到数据库树结构

我在PHP中使用Telnet连接到交换机的数据结构如下所示:

Array

(

[172.1.1.2] => Array

(

[0] => Array

(

[IP] => 172.1.1.1

[PlatformBrand] => dlink

)

[1] => Array

(

[IP] => 172.1.1.5

[PlatformBrand] => dlink

)

[2] => Array

(

[IP] => 172.1.1.7

[PlatformBrand] => dlink

)

[3] => Array

(

[IP] => 172.1.1.8

[PlatformBrand] => dlink

)

)

[172.1.1.6] => Array

(

[0] => Array

(

[IP] => 172.1.1.10

[PlatformBrand] => dlink

)

)

[172.1.1.7] => Array

(

[0] => Array

(

[IP] => 172.1.1.11

[PlatformBrand] => dlink

)

[1] => Array

(

[IP] => 172.1.1.14

[PlatformBrand] => dlink

)

)

)但我想把它转换成像这样的结构树:

CREATE TABLE `network_equipment_class` (

`id` int(11) NOT NULL,

`ip` varchar(15) NOT NULL,

`parent` int(11) NOT NULL,

`sort` int(11) NOT NULL

) ENGINE=InnoDB DEFAULT CHARSET=utf8;

id | ip | parent | sort

--------------------------------

1 | 172.1.1.2 | 0 | 0

2 | 172.1.1.1 | 1 | 0

3 | 172.1.1.5 | 1 | 1

4 | 172.1.1.7 | 1 | 2

5 | 172.1.1.8 | 1 | 3

6 | 172.1.1.6 | 0 | 1

7 | 172.1.1.10 | 6 | 0

8 | 172.1.1.11 | 4 | 0

9 | 172.1.1.14 | 4 | 1对于PHP> = 5.3来书写功能代码,任何想法或任何建议?

更新:

这是我的书面代码,但不能使用:

function createDBTree($IP, $dbTree){

if (!is_array($dbTree) || $dbTree == array()) {

$dbTree = array('ip' => $IP);

} else {

foreach ($dbTree as $dbkey => $tmpdbvalue) {

if(is_array($tmpdbvalue) && $arr !== array()) {

if (!isset($tmpdbvalue['children'])) $dbTree = $this->createDBTree($IP, $tmpdbvalue['children']);

} else {

if ($tmpdbvalue['ip'] == $IP) $tmpdbvalue['children'] = array('ip' => $IP);

}

}

}

return $dbTree;

}最后,我会完成这个结构:

172.1.1.2 => 172.1.1.1

=> 172.1.1.5

=> 172.1.1.7 => 172.1.1.11 => ... => ...

=> 172.1.1.14

=> ...

=> 172.1.1.8

=> ...

172.1.1.6 => 172.1.1.10

=> ...

...

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值