본문으로 바로가기

트리 만들기

category PHP 2016. 2. 17. 09:57

개발 중에 트리 구조가 필요해서 여기저기 글을 조합해 작성 하였다.

?doc= 에 따라 해당 트리를 찾아 불러오는 구조

<?php

/* 트리 시작 */

function buildTree(array $dataset) {

    $tree = array();


    /* Most datasets in the wild are enumerative arrays and we need associative array

       where the same ID used for addressing parents is used. We make associative

       array on the fly */

    $references = array();

    foreach ($dataset as $id => &$node) {

        // Add the node to our associative array using it's ID as key

        $references[$node['id']] = &$node;


        // Add empty placeholder for children

        $node['children'] = array();


        // It it's a root node, we add it directly to the tree

        if (is_null($node['parentId'])) {

            $tree[$node['id']] = &$node;

        } else {

            // It was not a root node, add this node as a reference in the parent.

            $references[$node['parentId']]['children'][$node['id']] = &$node;

        }

    }


    return $tree;

}

/* 트리 끝 */


/* 트리 작성을 위한 데이터 밀어 넣기 시작 */

$map = array(

   array('id' => '0','name' => 'HOME'),

    array('id' => 'about', 'parentId' => '0', 'name' => '대제목','doc' => 'sub_01', 'class' => 'top_color_01'),

    array('id' => 'about_01', 'parentId' => 'about', 'name' => '소제목-1','doc'=>'sub_01' ),

    array('id' => 'about_02', 'parentId' => 'about', 'name' => '소제목-2','doc'=>'sub_01_02' ),

    array('id' => 'about_03', 'parentId' => 'about', 'name' => '소제목-3','doc'=>'sub_01_03' ),

array('id' => 'news', 'parentId' => '0', 'name' => '대제목1','doc' => 'sub_05' , 'class' => 'top_color_05'),

    array('id' => 'news_01', 'parentId' => 'news', 'name' => '소제목1-1','doc'=>'sub_05' ),

    array('id' => 'news_02', 'parentId' => 'news', 'name' => '소제목1-2','doc'=>'sub_05_02' )

);

/* 트리 작성을 위한 데이터 밀어 넣기 끝 */

//echo '<pre>';

//print_r(buildTree($map));

//echo '</pre>';

/* 트리 생성 시작*/

$site_map = buildTree($map);

/* 트리 생성 끝 */


/* 여기 부터 는 개인적으로 만들어 사용 하기 위해 작성 */

$tmp_menu_arr = $site_map['0']['children'];

//아래 부터 는 doc 타입이라는 값에 따라 소 메뉴 들을 담기 위해 사용

$sub_top_info['top_right_src'] = '';

$sub_top_info['top_left_src'] = '';

$sub_top_info['sub_top_class'] = '';

if($_REQUEST['doc']) $sub_doc = $_REQUEST['doc'];

$menu_tag = $sub_doc;

$top_menu_key = '';

$sub_menu = array();

foreach($tmp_menu_arr as $key => $value) {

if(strpos ($menu_tag,$value['doc']) !== false ) {

$top_menu_key = $value['id'];

$sub_menu = $tmp_menu_arr[$top_menu_key]['children'];

break;

}

}

// doc의 전,후 값 추출

$current = null;

$next = null;

$prev = null;

$keys = array_keys($sub_menu);

foreach(array_keys($keys) AS $k ){

    $this_value = $sub_menu[$keys[$k]];

    $nextval = $sub_menu[$keys[$k+1]];

    $prevval = $sub_menu[$keys[$k-1]];

if($this_value['doc'] == $sub_doc) {

$current = $this_value;

$next = $nextval?:$sub_menu[reset($keys)];

$prev = $prevval?:$sub_menu[end($keys)];

break;

}

}

// doc의 값 추출

$sub_top_info['title'] = $tmp_menu_arr[$top_menu_key]['name'];

$sub_top_info['sub_title'] = $current['name'];

$sub_top_info['top_right_src'] = "/?doc={$next['doc']}".$next['param']?:'';

$sub_top_info['top_left_src'] = "/?doc={$prev['doc']}".$prev['param']?:'';

$sub_top_info['sub_top_class'] = $tmp_menu_arr[$top_menu_key]['class'];

?>