使用PHP

时间:2016-01-01 06:33:05

标签: php arrays loops multidimensional-array tree

我想创建一个处理这个数组的函数

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

进入像

这样的树数组
$output = array (
        'Science' => array(
            'Engineering' => array(
                'Chemical Engineering' => array(
                )  
            )
         )
    );

我想知道这是否可能使用循环技术,即使级别数量不固定(变量)或未知也可以工作,以便可以动态创建输出。我需要这个来处理数据库数据到类别树数组。

到目前为止,这就是我所拥有的:

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

}

但是这不会返回我想要的$ output结构,这是典型的类别树。

4 个答案:

答案 0 :(得分:1)

https://stackoverflow.com/a/17189190/1679311的帮助下。我认为它应该是这样的:

$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);

//输出

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

                )

        )

)

答案 1 :(得分:1)

根据需要动态数组。试试这个让我知道

<?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);
?>

输出:

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

答案 2 :(得分:0)

我不知道你在尝试什么。我是如何实现您的预​​期输出的。让我知道

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

输出:

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

答案 3 :(得分:0)

基于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 ) );
}

输出

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

                [Industrial Engineering] => Array
                    (
                    )

            )

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

            )

    )

)