2

i have the tree solution but i want to convert it into array ...

 function generatePageTree($datas , $parent = 0)
$tree = '<ul>';
for($i=0, $ni=count($datas); $i < $ni; $i++){
    if($datas[$i]['parent_id'] == $parent){
        $tree .= '<li>';
        $tree .= $datas[$i]['ledger_account_name'];
        $tree .= $this->generatePageTree($datas, $datas[$i]['ledger_account_id']);
        $tree .= '</li>';
    }
}
    $tree .= '</ul>';
return $tree;
}

i want this tree structure in form of array.. can any one already done please help.... array format............

 array(
      id=>100, parentid=>0, name=>'a', children=>array(
        id=>101, parentid=>100, name=>'a', children=>array(
          id=>102, parentid=>101, name=>'a',
          id=>103, parentid=>101, name=>'a',
        )
      )
    )
3
  • so what is your desired array format? Commented May 13, 2015 at 4:56
  • hey thank you for your help , but i found the answer ... Commented May 13, 2015 at 5:49
  • Please let me know the answer. Commented Jun 3, 2017 at 16:41

1 Answer 1

2
function buildTree(array $elements, $parentId = 0) {
          $branch = array();

          foreach ($elements as $element) {

              if ($element['parent_id'] == $parentId) {
                  $children = $this->buildTree($elements, $element['ledger_account_id']);

                  if ($children) {

                      $element['children'] = $children;

                  }
                  $branch[$element['ledger_account_id']] = $element;
              }
          }

          return $branch;
      }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.