简体   繁体   中英

Dynamically creating nested/tree arrays using loop in PHP

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.

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
                        (
                        )

                )

        )

)

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 ( ) ) ) )

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] => ) ) )

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
                    (
                    )

            )

    )

)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM