0

I want to create a function that will process this array

$keys = array(
           array(
             'lev1' => 'Science',
             'lev2' => 'Engineering',
             'lev3' => 'Chemical Engineering'
           )
        );

into a tree array like

$output = array (
        'Science' => array(
            'Engineering' => array(
                'Chemical Engineering' => array(
                )  
            )
         )
    );

I am wondering if this is possible using a loop technique, something that will work even if the number of levels is not fixed (a variable) or unknown so that the output can be created dynamically. I need this for processing database data into a category tree array.

So far, this is what I have:

function build_category_tree( $keys, $output ) {

        for ( $x=1; $x<=3; $x++ ) {

            if ( $keys ) {

                $level_key = 'lev' . $x; 
                $id = $keys[$level_key];  

                if ( ! isset( $r[$id] ) ) { 
                    $output[$id] = array( '' );
                }
            }    
        }

       return $output;
}

// Implement the function
$keys = array(
            array(
               'lev1' => 'Science',
               'lev2' => 'Engineering',
               'lev3' => 'Chemical Engineering'
             )
         );

foreach( $keys as $k ) {

    $r = build_category_tree( $k, $r );

}

But this does not return my desired $output structure which is typical of a category tree.

4
  • does array has only 3 levels ? Commented Jan 1, 2016 at 6:55
  • yes, for this purpose it only has 3 Commented Jan 1, 2016 at 6:57
  • array( 'lev1' => 'Science', 'lev2' => 'Engineering', 'lev3' => 'Chemical Engineering' , 'lev3'=>'cse' ) .will same level comes again ? Commented Jan 1, 2016 at 6:58
  • No, they will appear only once. Each node is unique at all levels. The structure I am trying to produce is the above-mentioned $output array. The indices in $keys example are also unique. Basically, I am aiming to create a function that will create $output from $keys Commented Jan 1, 2016 at 7:03

4 Answers 4

1

Dynamic array as required. try this let me know

<?php
$temp = array();
foreach( $keys as $val => $second) { 
    foreach( $second as $k => $v) {         
         $k.'->'.$v; 
        $temp = array($v => $temp);
    }            
} 
$a=array_reverse($temp);
print_r($temp);
?>

output :

Array ( [Chemical Engineering] => Array ( [Engineering] => Array ( [Science] => Array ( ) ) ) )
Sign up to request clarification or add additional context in comments.

2 Comments

array_reverse is not working in my case too. science should be the fist key according to question.
@MahaDev : same situation :(. any how he itself solved his question
0

I dont know for what you are trying it. Any how I achieved your expected output. let me know

<?php
foreach ($keys as $row) {
    $l1= $row['lev1'];
    $l2=$row['lev2'];
    $l3= $row['lev3'];
}
$new_key[$l1][$l2][$l3]="";
print_r($new_key);
?>

output :

 Array ( [Science] => Array ( [Engineering] => Array ( [Chemical Engineering] => ) ) )

7 Comments

Not the generalized way. If the level increases more than 3 levels, your code will fail.
@MahaDev read the comments also. i too asked him. he said level wont increased
@MahaDev i will satisfy you also dont worry
Okay sorry, code works fine for three levels. Nice work. (Y)
Thank you. I am trying to avoid that technique and hope there is another way via loop to accomplish it without manually encoding the three keys to build the array. I am wondering if there is more dynamic way to do it.
|
0

Based on https://stackoverflow.com/a/17189190/1679311

function catree ( $keys, $res) {

   $t = &$res;        

   foreach ( $keys as $k ) {  

      if ( empty($t[$k] ) ) {  
         $t[$k] = array();   
      }

      $t = &$t[$k];      
  }

  unset($t); 
  return $res;
}

$keys = array (
         array (
        'lev1' => 'Science',
        'lev2' => 'Engineering',
        'lev3' => 'Chemical Engineering'
        ),
        array(
        'lev1' => 'Science',
        'lev2' => 'Engineering',
        'lev3' => 'Industrial Engineering'
        ),
        array(
        'lev1' => 'Science',
        'lev2' => 'Physics',
        'lev3' => 'Practical Physics'
        )
    );

$res = array();
foreach( $keys as $k ) {  
   $res = array_merge( $res, catree( $k , $res ) );
}

The output is

Array
(
[Science] => Array
    (
        [Engineering] => Array
            (
                [Chemical Engineering] => Array
                    (
                    )

                [Industrial Engineering] => Array
                    (
                    )

            )

        [Physics] => Array
            (
                [Practical Physics] => Array
                    (
                    )

            )

    )

)

Comments

0

With the help of https://stackoverflow.com/a/17189190/1679311. I think it should be like this:

$keys = array(
    array(
        'lev1' => 'Science',
        'lev2' => 'Engineering',
        'lev3' => 'Chemical Engineering'
    )
);

$output_array = array();
foreach( $keys as $val => $second) { 
    $second = array_reverse($second);
    foreach( $second as $k => $v) {
        $output_array = array($v => $output_array);
    }            
} 
echo '<pre>';
print_r($output_array);

//output

Array
(
    [Science] => Array
        (
            [Engineering] => Array
                (
                    [Chemical Engineering] => Array
                        (
                        )

                )

        )

)

1 Comment

I am actually aiming for Science as the topmost key in the node ( Chemical Engineering is under Engineering. Engineering is under Science )

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.