Home  >  Article  >  Backend Development  >  PHP反向递归

PHP反向递归

WBOY
WBOYOriginal
2016-06-06 20:23:061993browse

需求是这样的,原数组如下,通过chrome配合phpview的插件截图(二维数组)
PHP反向递归

经过“反向递归后”需要展现成如下树状结构。
其中parent_id是level为上一级的id(最顶级的level为0),比如parent_id=125的用户level为1,那么他的上一级的id是125,先谢过了
PHP反向递归

回复内容:

需求是这样的,原数组如下,通过chrome配合phpview的插件截图(二维数组)
PHP反向递归

经过“反向递归后”需要展现成如下树状结构。
其中parent_id是level为上一级的id(最顶级的level为0),比如parent_id=125的用户level为1,那么他的上一级的id是125,先谢过了
PHP反向递归

不好意思,回答晚了,做了个稍微复杂点的,用了一个内部排序:

<code>// $formated_arr 是你的输入对象
// 首先按照level做一次排序
usort($formated_arr, function($a, $b) {
    $al = intval($a['level']);
    $bl = intval($b['level']);
    return ($al > $bl) ? 1 : -1;
});

// 因为上面做过排序了,所以这里虽然是递归,但对数组只遍历了一次
function reformat_tree(&$arrTmp, $parent_id=0) {
    $ret = null;
    foreach ($arrTmp as $k => $v) {
        if($v['parent_id'] == $parent_id) {
            $ret[$v['id']] = $v;
            unset($arrTmp[$k]);
            $child = reformat_tree($arrTmp, $v['id']);
            !is_null($child) ? $ret[$v['id']]['child'] = $child : 1;
        }
    }
    return $ret;
}

echo(json_encode(reformat_tree($formated_arr)));</code>

试试我这个:

<code>function createMenuTree($data = array(), $pid = 0){
    if (empty($data)){
        return array();
    }
     static $level = 1;
     $returnArray = array();
    foreach ($data as $node){
        if ($node['parent_id'] == $pid){
            $returnArray[] = array(
                'cat_id'   => $node['cat_id'],
                'cat_name' => $node['cat_name'],
                'level' => $level,
                'parent_id' => $node['parent_id'],
                'show_in_nav' => $node['show_in_nav'],
                'is_show' => $node['is_show'],
                'sort_order' => $node['sort_order']
                );
            if (hasChild($node['cat_id'], $data)){
               $level++;
               $returnArray = array_merge($returnArray, createMenuTree($data, $node['cat_id']));
               $level--;
            }
        }
    }
     return $returnArray;
}

function hasChild($cid, $data){
    $hasChild = false;
    foreach ($data as $node){
        if ($node['parent_id'] == $cid){
            $hasChild = true;
            break;
        }
    }
    return $hasChild;
}
 </code>

字段跟你的不是很一样,但是思路好像跟你想要的差不多,你可以自己拿去修改一下。

<code>function recursive_tree($arr,$level = 0,$parent_id = 0){
    $tmp = array();       
    foreach ($arr as $key => $value) {
        if($value['level'] == $level && $value['parent_id'] == $parent_id){
            $arr[$key]['child'] = recursive_tree($arr,$value['level'] + 1,$value['id']);
            if(empty($arr[$key]['child'])){
                unset($arr[$key]['child']);
            }
            $tmp[] = $arr[$key];
        }
    }
    return $tmp;
}


$arr = array(
    array(
        'id'     => '125',
        'level' => '0',
        'user_id' => '1021',
        'parent_id' => '0', 
    ),
    array(
        'id'     => '189',
        'level' => '1',
        'user_id' => '1022',
        'parent_id' => '125', 
    ),
    array(
        'id'     => '425',
        'level' => '2',
        'user_id' => '4119',
        'parent_id' => '189', 
    ),
    array(
        'id'     => '385',
        'level' => '3',
        'user_id' => '3170',
        'parent_id' => '425', 
    ),
    array(
        'id'     => '782',
        'level' => '3',
        'user_id' => '5698',
        'parent_id' => '425', 
    ),
    array(
        'id'     => '688',
        'level' => '1',
        'user_id' => '7045',
        'parent_id' => '125', 
    )
);

echo json_encode(recursive_tree($arr,0,0));die;</code>

http://segmentfault.com/q/1010000004052822/a-1020000004055246

参照我在这个问题下的回答的前半部分,详细解释了题主的问题所需要的那个并不复杂的算法,只是题目从OC变成了PHP。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn