donglinxin8765 2011-10-29 12:11
浏览 17
已采纳

如何将字符串分解为多级数组?

I was wondering if it's possible to do something like this.

I'm having the following string:

Parent1@MiddleA%Child|Child|Child|MiddleB%Child|Child|Child|Parent2@MiddleA%|Child

And I'd like to explode it into something like:

-Parent1
---MiddleA
------Child
------Child
------Child
---MiddleB
------Child
------Child
------Child
-Parent2
---MiddleA
------Child

I don't understand how to explode it and then explode it again to create an output like above.

The idea is that every parent will be identified with a trailing @, every child of that parent will have a trailing % and every child of that child will have a trailing|.

  • 写回答

3条回答 默认 最新

  • dongzecai0684 2011-10-29 12:32
    关注

    The data you give as input in your question:

    Parent1@MiddleA%Child|Child|Child|MiddleB%Child|Child|Child|Parent2@MiddleA%|Child
    

    is not really well fitting to produce the output you ask for. It's even syntactically incorrect at it's end, the delimiters in MiddleA%|Child specifically.

    Correnting this, you can easily do it with preg_split:

    $str = 'Parent1@MiddleA%Child|Child|Child|MiddleB%Child|Child|Child|Parent2@MiddleA%Child|';
    
    $elements = preg_split('~([^@%|]+?[@%|])~', $str, 0, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
    
    $types = '@%|';
    foreach($elements as $element)
    {
        $label = substr($element, 0, -1);
        $type = substr($element, -1);
        $indentValue = strpos($types, $type);
        if (FALSE === $indentValue) throw new Exception('Invalid string given.');
        $indent = str_repeat('-', $indentValue * 2 + 1);
        printf("%s %s
    ", $indent, $label);
    }
    

    If you don't have the input string in a valid format, you need to fix it first, e.g. with an appropriate parser or you need to react on the bogus cases inside the foreach loop.

    Edit: This is an modified example that turns the string into a tree-like structure so you can output it with nested foreach loops:

    $str = 'Parent1@MiddleA%Child|Child|Child|MiddleB%Child|Child|Child|Parent2@MiddleA%Child|';
    
    $types = '@%|';
    $pattern = sprintf('~([^%1$s]+?[%1$s])~', $types);
    $elements = preg_split($pattern, $str, 0, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
    
    $tree = array();
    foreach($elements as $element)
    {
        $label = substr($element, 0, -1);
        $type = substr($element, -1);
        $indentValue = strpos($types, $type);
        if (FALSE === $indentValue) throw new Exception('Invalid string given.');
    
        $add =& $tree;
        for($i = $indentValue; $i--;)
        {
            $index = max(0, count($add)-1);
            $add =& $add[$index][1];
        }
        $add[] = array($label, array());
        unset($add);
    }
    
    foreach($tree as $level1)
    {
        echo '<div>', $level1[0], "
    ";
        foreach($level1[1] as $level2)
        {
            echo '  <h3>', $level2[0], '</h3>', "
    ";
            foreach($level2[1] as $level3)
            {
                echo '    <span>', $level3[0],'</span>', "
    ";
            }
        }
        echo '</div>', "
    ";
    }
    

    (Demo) - Hope this is helpful.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启