php 一维数组按条件转换成二维数组 5
如:array(4){[0]=>array(4){["id"]=>string(1)"2"["sort"]=>string(1)"2"["navname"]=>strin...
如:
array(4) {
[0] => array(4) {
["id"] => string(1) "2"
["sort"] => string(1) "2"
["navname"] => string(12) "常见问题"
["parentsid"] => string(1) "0"
}
[1] => array(4) {
["id"] => string(1) "3"
["sort"] => string(1) "3"
["navname"] => string(12) "配置接口"
["parentsid"] => string(1) "0"
}
[2] => array(4) {
["id"] => string(1) "4"
["sort"] => string(1) "4"
["navname"] => string(12) "问题一"
["parentsid"] => string(1) "2"
}
[3] => array(4) {
["id"] => string(1) "4"
["sort"] => string(1) "4"
["navname"] => string(12) "配置接口一"
["parentsid"] => string(1) "3"
}}
需要将id和parentid匹配,匹配对应的将含有parentid的作为子级,在一级数组中添加一个键值形成:
[0] => array(4) {
["id"] => string(1) "2"
["sort"] => string(1) "2"
["navname"] => string(12) "常见问题"
["parentsid"] => string(1) "0"
["subnav"]=>array(4){
["id"] => string(1) "4"
["sort"] => string(1) "4"
["navname"] => string(12) "问题一"
["parentsid"] => string(1) "2"
}
}
谢谢 展开
array(4) {
[0] => array(4) {
["id"] => string(1) "2"
["sort"] => string(1) "2"
["navname"] => string(12) "常见问题"
["parentsid"] => string(1) "0"
}
[1] => array(4) {
["id"] => string(1) "3"
["sort"] => string(1) "3"
["navname"] => string(12) "配置接口"
["parentsid"] => string(1) "0"
}
[2] => array(4) {
["id"] => string(1) "4"
["sort"] => string(1) "4"
["navname"] => string(12) "问题一"
["parentsid"] => string(1) "2"
}
[3] => array(4) {
["id"] => string(1) "4"
["sort"] => string(1) "4"
["navname"] => string(12) "配置接口一"
["parentsid"] => string(1) "3"
}}
需要将id和parentid匹配,匹配对应的将含有parentid的作为子级,在一级数组中添加一个键值形成:
[0] => array(4) {
["id"] => string(1) "2"
["sort"] => string(1) "2"
["navname"] => string(12) "常见问题"
["parentsid"] => string(1) "0"
["subnav"]=>array(4){
["id"] => string(1) "4"
["sort"] => string(1) "4"
["navname"] => string(12) "问题一"
["parentsid"] => string(1) "2"
}
}
谢谢 展开
4个回答
展开全部
//根据你的需求,我写了个测试程序,你整理下就可以用了
$array = array(
array('id'=>1,"parentsid"=>0),
array('id'=>2,"parentsid"=>0),
array('id'=>3,"parentsid"=>2),
array('id'=>4,"parentsid"=>2),
array('id'=>5,"parentsid"=>1),
array('id'=>6,"parentsid"=>1)
);
$newArray = array();
foreach($array as $row){
if($row['parentsid'] == 0){
$newArray[] = $row;
}
}
foreach($array as $row){
foreach($newArray as $key=>$newRow){
if($row['parentsid'] == $newRow['id']){
$newArray[$key]['subnav'][] = $row;
}
}
}
展开全部
写个函数,也是来自于tp官方
function list_to_tree($list, $pk='id', $pid = 'pid', $child = '_child', $root = 0) {
// 创建Tree
$tree = array();
if(is_array($list)) {
// 创建基于主键的数组引用
$refer = array();
foreach ($list as $key => $data) {
$refer[$data[$pk]] =& $list[$key];
}
foreach ($list as $key => $data) {
// 判断是否存在parent
$parentId = $data[$pid];
if ($root == $parentId) {
$tree[] =& $list[$key];
}else{
if (isset($refer[$parentId])) {
$parent =& $refer[$parentId];
$parent[$child][] =& $list[$key];
}
}
}
}
return $tree;
}
然后定义一维数组为$list,然后 print_r(list_to_tree($list,"id","parentsid","subnav"));
function list_to_tree($list, $pk='id', $pid = 'pid', $child = '_child', $root = 0) {
// 创建Tree
$tree = array();
if(is_array($list)) {
// 创建基于主键的数组引用
$refer = array();
foreach ($list as $key => $data) {
$refer[$data[$pk]] =& $list[$key];
}
foreach ($list as $key => $data) {
// 判断是否存在parent
$parentId = $data[$pid];
if ($root == $parentId) {
$tree[] =& $list[$key];
}else{
if (isset($refer[$parentId])) {
$parent =& $refer[$parentId];
$parent[$child][] =& $list[$key];
}
}
}
}
return $tree;
}
然后定义一维数组为$list,然后 print_r(list_to_tree($list,"id","parentsid","subnav"));
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2014-10-20
展开全部
function made_array($parent_id){
$str=array();
$arrays = get_arrays($parent_id);
foreach($arrays as $arr){
if(made_array($arr['id'])){
$arr['subnav']=made_array($arr['id']);
}
$str[]=$arr;
}
return $str;
}
function get_arrays($parent_id){
//返回parentsid=$parent_id的数组
}
made_array(0)就是结果
$str=array();
$arrays = get_arrays($parent_id);
foreach($arrays as $arr){
if(made_array($arr['id'])){
$arr['subnav']=made_array($arr['id']);
}
$str[]=$arr;
}
return $str;
}
function get_arrays($parent_id){
//返回parentsid=$parent_id的数组
}
made_array(0)就是结果
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
写个递归方法就可以了。
追问
我就是不太清楚怎么写这个递归方法,有没有简单例子,或者你能否帮我写下,我参考下,谢谢
追答
$d = array(
'0' => array (
'id' => "2",
'sort' => "2",
'navname' => "常见问题",
'parentsid' => "0",
),
'1' => array (
'id' => "3",
'sort' => "3",
'navname' => "配置接口",
'parentsid' => "0",
),
'2' => array (
'id' => "4",
'sort' => "4",
'navname' => "问题一",
'parentsid' => "2",
),
'3' => array (
'id' => "4",
'sort' => "4",
'navname' => "配置接口一",
'parentsid' => "3",
),
);
$data = build_tree($d);
print_r($data);
function build_tree($dat,$parentsid='0'){
$rs = array();
foreach($dat as $val){
if($val['parentsid']==$parentsid){
$subnav = build_tree($dat,$val['id']);
if($subnav){
$val['subnav'] = $subnav;
}
array_push($rs,$val);
}
}
return $rs;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询