dongrang2140 2015-04-16 17:36
浏览 21
已采纳

为什么深度为1的行添加到数组[1]

I'm using the following functions to convert a tabbed list to array, but tabs with depth of 1 are added to array[1];

The tab list:

    $test = "Test
        Sub Item Test 1 (Test)
        Sub Item Test 2 (Test)
                Sub Sub Item Test 1 (Sub Item Test 2)
            Sub Sub Sub Item Test 1 (Sub Item Test 2)
    Test 2
        Sub Item Test 1 (Test 2)
    Test 3
        Sub Item Test 1 (Test 3)
    Test 4
        Sub Item Test 1 (Test 4)
    Test 5
        Sub Item Test 1 (Test 5)";

The functions:

    function convert( $list, $indent = "\t" ) {

        $main          = array();

        foreach ( explode( PHP_EOL, $list ) as $line ) {
            $depth = substr_count( $line, $indent );
            $line  = trim( $line );
            if ( $line == '' ) {
                continue;
            }

            $a   = &buildArray( $main, $depth, 0, $line );
            $a[$i++] = $line;
        }

        return $main;

    }

    function &buildArray( &$array, $depth, $current = 0, $line = null ) {

        if ( $depth == $current ) {
            return $array;
        } else {
            foreach ( $array as &$value ) {
                if ( is_array( $value ) ) {
                    return buildArray( $value, $depth, ++ $current );
                }
            }

            $tmp  = array();
            $array[] = &$tmp;

            return buildArray( $tmp, $depth, ++ $current );
        }
    }

Here is the result after running "convert($test)"

    Array
    (
        [0] => Test
        [1] => Array
            (
                [1] => Sub Item Test 1 (Test)
                [2] => Sub Item Test 2 (Test)
                [3] => Array
                    (
                        [0] => Array
                            (
                                [3] => Sub Sub Item Test 1 (Sub Item Test 2)
                            )

                        [4] => Sub Sub Sub Item Test 1 (Sub Item Test 2)
                    )

                [6] => Sub Item Test 1 (Test 2)
                [8] => Sub Item Test 1 (Test 3)
                [10] => Sub Item Test 1 (Test 4)
                [12] => Sub Item Test 1 (Test 5)
            )

        [5] => Test 2
        [7] => Test 3
        [9] => Test 4
        [11] => Test 5
    )

So why do I get array[1][6] instead of array[6], and so on for all the lines after array[5]?

  • 写回答

1条回答 默认 最新

  • dongmao4486 2016-01-09 11:24
    关注

    I think it should be easier if you re-design how you will store the data. My suggestion is that for every level create an array with key for the label and another key for the sublevels.

    $test = "Test 1
    
    \tSub Item Test 1
    \t\tSub Sub Item Test 1
    \t\tSub Sub Item Test 2
    \tSub Item Test 2
    \tSub Item Test 3
    \tSub Item Test 4
    Test 2
    
    \tSub Item Test 1
    \tSub Item Test 2
    \tSub Item Test 3
    \tSub Item Test 4
    \t\tSub Sub Item Test 1
    \t\tSub Sub Item Test 2";
    

    Then it should look something like this after:

    Array (
        [0] => Array (
            ['label']    => Test 1
            ['children'] => array(
                [0] => Array (
                    ['label']    => Sub Item Test 1 (Test 1)
                    ['children'] => array()
                )
                [1] => Array (
                    ['label']    => Sub Item Test 2 (Test 1)
                    ['children'] => array(
                        [0] => Array (
                            ['label']    => Sub Sub Item Test 1 (Sub Item Test 2)
                            ['children'] => array()
                        )
                        [1] => Array (
                            ['label']    => Sub Sub Item Test 2 (Sub Item Test 2)
                            ['children'] => array()
                        )
                    )
                )
                [2] => Array (
                    ['label']    => Sub Item Test 3 (Test 1)
                    ['children'] => array()
                )
            )
        [1] => Array (
            ['label']    => Test 2
            ['children'] => array(
                [0] => Array (
                    ['label']    => Sub Item Test 1 (Test 2)
                    ['children'] => array()
                )
                [1] => Array (
                    ['label']    => Sub Item Test 2 (Test 2)
                    ['children'] => array()
                )
                [2] => Array (
                    ['label']    => Sub Item Test 3 (Test 2)
                    ['children'] => array()
                )
                [3] => Array (
                    ['label']    => Sub Item Test 4 (Test 2)
                    ['children'] => array(
                        [0] => Array (
                            ['label']    => Sub Sub Item Test 4 (Sub Item Test 4)
                            ['children'] => array()
                        )
                        [1] => Array (
                            ['label']    => Sub Sub Item Test 4 (Sub Item Test 4)
                            ['children'] => array()
                        )
                    )
                )
            )
        )
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系