hierarchical structure in array, recursion in php

542

i have hierarchical structure in array like this:

item root 1
- item child 1
-- item child 2
 item root 2
- item child

i want to get:

1. item root 1
 1-1. item child 1
 1-1-1. item child 2
2. item root 2
 2-1. item child

My function to build tree

function printTree($data, $level = 0, $counter = 1) { 

            foreach ($data as $item) {  

                if ($item['parent_id'] == 0) {
                    $addr =  $counter . '. ' . $item['address'];
                    $counter++;
                }

                else if ($item['parent_id'] != 0) {                        

                   $addr = $counter . '-' . $counter . ' ' . $item['address'];                   



                } else {
                     $addr = $item['address'];              
                }

                global $result;

                $result['aaData'][] = Array(
                 $addr,
                 $item['mask'],
                 $item['status'],
                 $item['subnets'],
                 $item['therange'] = $item['start'] . ' - ' . $item['end'],
                 $item['type'],
                 $item['node'],
                 $item['id'],                               
             );   

                if (isset($item['children'])) {                    
                    printTree($item['children'], $level + 1, $counter + 1);
                }


            }

            return $result;        
        }

But my result is incorrect, root's element count normal, but childs wrong, how can i fix this? need help, thanks!

604

Answer

Solution:

Here is some code, with your specific test case:

EDIT (corrected with $counter, not id)

function makeTree($data, $pre='') {
  $result = array();
  $counter = 1;

  if (is_array($data)) foreach ($data as $item) {
    $result[] = $pre.$counter.' '.$item['address'];
    if (isset($item['children']) && is_array($item['children'])) {
      $result = array_merge($result, makeTree($item['children'], $pre.$counter.'-'));
    }
    $counter++;
  }

  return $result;
}

$data = array(
  array(
    'id' => 1,
    'address' => 'addy 1',
    'children' => array(
      array(
        'id' => 1,
        'address' => 'addy 1.1',
        'children' => array(
          array(
            'id' => 1,
            'address' => 'addy 1.1.1',
          ),
        ),
      ),
    ),
  ),
  array(
    'id' => 2,
    'address' => 'addy 2',
    'children' => array(
      array(
        'id' => 1,
        'address' => 'addy 2.1'
      ),
    ),
  )
);

$out_array = makeTree($data);
print_r($out_array);

This is just a basic recursive function that performs a very similar task. It is not the full solution. Put your specific logic in there to create the final output you need, but the basic 'counter' idea you ask for works.

Hope this helps.

People are also looking for solutions to the problem: php - Javascript: Uncaught SyntaxError: Unexpected token ILLEGAL

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.