1

こんなことができるのかなぁと思っていました。

私は次の文字列を持っています:

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

そして、私はそれを次のようなものに爆発させたいと思います:

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

分解してからもう一度分解して上記のような出力を作成する方法がわかりません。

アイデアは、すべての親が末尾@で識別され、その親のすべての子が末尾を持ち、%その子のすべての子が末尾を持つということ|です。

4

3 に答える 3

0
<?php
$string = "Parent1@MiddleA%Child|Child|Child|MiddleB%Child|Child|Child|Parent2@MiddleA%|Child";

$explode1 = explode ("@",$string);

foreach ($explode1 as $str)
{

  $explode2 = explode("|", $str);

  foreach ($explode2 as $str2)
  {
    $explode3 = explode ("%", $str2);

        foreach($explode3 as $str3)
        {
        echo ($str3 != "") ? $str3 . "<br/>" : "";
        }
  }

}
?>

出力します:

Parent1
MiddleA
Child
Child
Child
MiddleB
Child
Child
Child
Parent2
MiddleA
Child
于 2011-10-29T12:23:06.877 に答える
0

多次元配列に自動的に分解する方法はないと思います。そのため、作業を行うには特定のアルゴリズムを作成する必要があります。文字列には論理シーケンスがあるため、簡単に変換できます。「@」で展開することから始め、次に各要素について「A%」で展開し、次に「|」で展開します。

于 2011-10-29T12:16:51.043 に答える
0

質問の入力として提供するデータ:

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

要求する出力を生成するのにあまり適していません。最後に構文的に正しくなく、MiddleA%|Child具体的には区切り文字です。

これを裏付けると、次のように簡単に行うことができます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\n", $indent, $label);
}

有効な形式の入力文字列がない場合は、最初にそれを修正する必要があります。たとえば、適切なパーサーを使用するか、foreachループ内の偽のケースに対応する必要があります。

編集:これは、文字列をツリーのような構造に変換して、ネストされたforeachループで出力できるようにする変更された例です。

$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], "\n";
    foreach($level1[1] as $level2)
    {
        echo '  <h3>', $level2[0], '</h3>', "\n";
        foreach($level2[1] as $level3)
        {
            echo '    <span>', $level3[0],'</span>', "\n";
        }
    }
    echo '</div>', "\n";
}

デモ)-これがお役に立てば幸いです。

于 2011-10-29T12:32:37.743 に答える