现在有个一维数组array(
arry('id' => 1,'pid' => 0,'text' => '1,'),
arry('id' => 2,'pid' => 1,'text' => '1,1'),
arry('id' => 3,'pid' => 1,'text' => '1,2'),
arry('id' => 4'pid' => 1,'text' => '1.3'),
arry('id' => 5,'pid' => 2,'text' => '1,1,1'),
arry('id' => 6,'pid' => 2,'text' => '1,1,2'),
arry('id' => 7,'pid' => 2,'text' => '1,1,3'),
arry('id' => 8,'pid' => 0,'text' => '2,'),
arry('id' => 9,'pid' => 0,'text' => '3,'),
)
把这个一维数组转成一个多维数组
arry('id' => 1,'pid' => 0,'text' => '1,'),
arry('id' => 2,'pid' => 1,'text' => '1,1'),
arry('id' => 3,'pid' => 1,'text' => '1,2'),
arry('id' => 4'pid' => 1,'text' => '1.3'),
arry('id' => 5,'pid' => 2,'text' => '1,1,1'),
arry('id' => 6,'pid' => 2,'text' => '1,1,2'),
arry('id' => 7,'pid' => 2,'text' => '1,1,3'),
arry('id' => 8,'pid' => 0,'text' => '2,'),
arry('id' => 9,'pid' => 0,'text' => '3,'),
)
把这个一维数组转成一个多维数组
转成下面这样的
array(
arry('id' => 1,'pid' => 0,'text' => '1,',
'children' => array(
arry('id' => 2,'pid' => 1,'text' => '1,1',
'children' => array(
arry('id' => 5,'pid' => 2,'text' => '1,1,1'),
arry('id' => 6,'pid' => 2,'text' => '1,1,2'),
arry('id' => 7,'pid' => 2,'text' => '1,1,3')
)
),
arry('id' => 3,'pid' => 1,'text' => '1,2'),
arry('id' => 4'pid' => 1,'text' => '1.3')
)),
arry('id' => 8,'pid' => 0,'text' => '2,'),
arry('id' => 9,'pid' => 0,'text' => '3,'),
)
array('id' => 1,'pid' => 0,'text' => '1,'),
array('id' => 2,'pid' => 1,'text' => '1,1'),
array('id' => 3,'pid' => 1,'text' => '1,2'),
array('id' => 4,'pid' => 1,'text' => '1.3'),
array('id' => 5,'pid' => 2,'text' => '1,1,1'),
array('id' => 6,'pid' => 2,'text' => '1,1,2'),
array('id' => 7,'pid' => 2,'text' => '1,1,3'),
array('id' => 8,'pid' => 0,'text' => '2,'),
array('id' => 9,'pid' => 0,'text' => '3,'),
);include 'lib/TTree.php';
$p = new TTree;
print_r($p->parse($ar, true));
exit;
asort($ar);
$out = $ar;
foreach ($out as $id => $item){
if ($item['pid']){
$out[$item['pid']]['children'][$item['id']] = &$out[$id];
}
}
print_r($out);Array
(
[1] => Array
(
[id] => 1
[pid] => 0
[text] => 1,
[deep] => 0
[child] => Array
(
[0] => Array
(
[id] => 2
[pid] => 1
[text] => 1,1
[deep] => 1
[child] => Array
(
[0] => Array
(
[id] => 5
[pid] => 2
[text] => 1,1,1
[deep] => 2
) [1] => Array
(
[id] => 6
[pid] => 2
[text] => 1,1,2
[deep] => 2
) [2] => Array
(
[id] => 7
[pid] => 2
[text] => 1,1,3
[deep] => 2
) ) ) [1] => Array
(
[id] => 3
[pid] => 1
[text] => 1,2
[deep] => 1
) [2] => Array
(
[id] => 4
[pid] => 1
[text] => 1.3
[deep] => 1
) ) ) [8] => Array
(
[id] => 8
[pid] => 0
[text] => 2,
[deep] => 0
) [9] => Array
(
[id] => 9
[pid] => 0
[text] => 3,
[deep] => 0
))
//error_reporting(E_ALL);
require_once 'TObject.php';class TTree extends TObject {
var $data = array();
/**
* 公共方法 parse
* 功能 从传入的数组构造树形数组
* 参数
* $ar 数组,待处理的数组
* $type 逻辑,指示生成结果的样式,=true 巢状数组, =false 二维数组(默认)
* $id 字符串或数值,指示待处理的数组中对应主键的列名
* $parent 字符串或数值,指示待处理的数组中对应父键的列名
**/
function parse(&$ar, $type=false, $id='id', $parent='pid') {
$out = $this->_tile($ar, $id, $parent);
$this->data = $out;
if(! $type) return $this->data;
foreach($out as $v) {
if($v['deep'] == 0) break;
}
return $this->data = $this->_search($out, $v[$parent], 0, $id, $parent);
}
function _search(&$ar, $value, $deep, $id='id', $parent='pid') {
$r = array();
foreach($ar as $v) {
if($v['deep'] == $deep && $v[$parent]===$value) {
$t = $v;
$t['child'] = $this->_search($ar, $v[$id], $v['deep']+1, $id, $parent);
$r[] = $t;
}
}
return $r;
}
/**
* 内部方法 _find
* 功能 在二维数组中搜索指定的键
* 返回 找到返回数组元素,否则返回false
* 参数
* $pid 数值、字符串,查询键
* $ar 数组
* $key 数值、字符串,待查找的元素名
* 说明 由内部方法_tile调用
**/
function _find($pid,&$ar, $key) {
if(is_numeric($pid)) {
foreach($ar as $k=>$t) {
if($t[$key] == $pid) {
unset($ar[$k]);
return $t;
}
}
}else {
foreach($ar as $k=>$t) {
if($t[$key] === $pid) {
unset($ar[$k]);
return $t;
}
}
}
return false;
}
/**
* 内部方法 _tile
* 功能 从传入的数组构造树形数组
* 用附加的deep元素表示层次
* 参数 $ar 数组,
*/
function _tile($ar, $id, $parent) {
$out = array();
$deep = 0;
$pid = array(0);
do {
if($t = $this->_find($pid[0], $ar, $parent)) {
$t['deep'] = $deep++;
$out[] = $t;
array_unshift($pid,$t[$id]);
}else {
$deep--;
array_shift($pid);
}
}while($deep >= 0);
//print_r($out);
return $out;
}
/**
* 信号接口
**/
function signal($bill) {
$id = isset($bill['id']) ? $bill['id'] : 'id';
$parent = isset($bill['pid']) ? $bill['pid'] : 'pid';
$type = ! empty($bill['type']);
return $this->parse($bill['data'], $type, $id, $parent);
}
}自己看着修改吧
$p = new TTree;
print_r($p->parse($ar, true)); 唠叨大哥,哪里有?,谢谢了。非常敬仰你的 $out[$item['pid']]['children'][$item['id']] = &$out[$id]; 这种写法,哈哈,虽然结果不对。