一维数组转多维数组问题。

象宇 2009-09-01 11:35:58
现在有个一维数组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,'),
)
把这个一维数组转成一个多维数组
...全文
85 点赞 收藏 8
写回复
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
象宇 2009-09-01
不好意思,是二维,,,转多维
转成下面这样的
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,'),
)
回复
江南昆虫 2009-09-01
晕,这不就是二维吗?

列出你需要的结果啊
回复
象宇 2009-09-01
非常感谢 唠叨 大哥!
回复
象宇 2009-09-01
include 'lib/TTree.php';
$p = new TTree;
print_r($p->parse($ar, true));

唠叨大哥,哪里有?我的邮箱是songtao1718@sina.com,谢谢了。

非常敬仰你的 $out[$item['pid']]['children'][$item['id']] = &$out[$id]; 这种写法,哈哈,虽然结果不对。
回复
xuzuning 2009-09-01
<?php
//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);
}
}

自己看着修改吧
回复
xuzuning 2009-09-01
$ar = array(
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
)

)
回复
shawnpeter 2009-09-01
我也顶!
回复
ms_X0828 2009-09-01
帮你顶一下...
回复
发动态
发帖子
基础编程
创建于2007-09-28

2.1w+

社区成员

从PHP安装配置,PHP入门,PHP基础到PHP应用
申请成为版主
社区公告
暂无公告