Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have following array:

Array
(
    [1] => 0
    [2] => 1
    [3] => 2
    [4] => 3
    [5] => 1
    [6] => 0
)

keys of this array is unique, and values showing parent of key. like parent of 1 & 6 is 0, parent of 2 is 1, for 3 is 2....

I was writing a recursive function which will find a tree view for given parent id. here is my code:

function recurviceChild($parent, $childParent, $resultArr = array()) {
        foreach ($childParent as $key => $parentId) {
            if ($parent == $parentId) {
                $resultArr[$parentId][] = $key;
                $resultArr = $this->recurviceChild($key, $childParent, $resultArr);
            }
        }
        return $resultArr;
    }

The function I created give me result for depth of level one. result of this function if i call it for $parent=1 ($childParent is array given above) is:

Array
(
    [1] => Array
        (
            [0] => 2
            [1] => 5
        )

    [2] => Array
        (
            [0] => 3
        )

    [3] => Array
        (
            [0] => 4
        )

)

I m expecting result like this:

 Array
        (
            [1] => Array
                (
                    [2] => Array
                       (
                           [3] => Array
                                (
                                   [0] => 4
                                )
                       )

                )
             [2] => 5
        )

or something that help me to create a tree view. Thank you in advance.

share|improve this question
2  
your expected array does not make sense to me.. what is 4 there?? – bipen Jul 11 at 12:01
i don't understand the logic of how you transform the original array to your expected array. whats the 4 in the end and whats 1 2 3? – DevZer0 Jul 11 at 12:01
in expected array keys are the parent of other values – Ravi Jul 11 at 12:02
I want array like this .. so in loop it can be used to generate ul li menu 1 -2 --3 ---4 5 – Ravi Jul 11 at 12:04
4 is the last comment who doesn't have child – Ravi Jul 11 at 12:05

2 Answers

up vote 1 down vote accepted

This does what you want:

<?php

$a= array
(
    1 => 0,
    2 => 1,
    3 => 2,
    4 => 3,
    5 => 1,
    6 => 0
);

class node {
    var $children;
    public function __construct(){
        $this->children = array();
    }
}

$tree = array();
foreach ($a as $q => $p){
    if(!isset($tree[$p]))
        $tree[$p] = new node;
    if(!isset($tree[$q]))
        $tree[$q] = new node;
    $mark[$p]=FALSE;
    $mark[$q]=FALSE;
    array_push($tree[$p]->children,$q);
}

function dfs(&$ans,$node){
    global $tree, $mark;
    $mark[$node] = TRUE;
    $ans = array();
    foreach($tree[$node]->children as $child)
        if(!$mark[$child]){
            $ans[$child]=$child;
            dfs($ans[$child],$child);
        }
}

$parent=1;

dfs($ans,$parent);

print_r($ans);

?>
share|improve this answer
thanks dude ..... – Ravi Jul 15 at 12:42

model:

class Menu extends CI_Model {

public function __construct() {
    parent::__construct();

}

public function menu_array($parent = 0) {
    $items = array();

    $this->db->where('parent', $parent);
    $results = $this->db->get('os_menu')->result();

    foreach($results as $result) {
        $child_array = $this->menu_array($result->id);
        if(sizeof($child_array) == 0) {
            array_push($items, $result);
        } else {
            array_push($items, array($result, $child_array));
        }
    }
    return $items;
}

public function show_menu_array($array){
    $output = '<ul>';
    foreach ($array as $key => $mixedValue) {
        if (is_array($mixedValue)) {
            $output .= '<li>' . $this->show_menu_array($mixedValue) . '</li>';
        } else {
            $output .= '<li>' . $mixedValue->name . '</li>';
        }
    }
    $output .= '</ul>';
    return $output;
}

}

view:

$menu = new Menu();
$menu_array = $menu->menu_array();
echo $menu->show_menu_array($menu_array);
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.