生成列表字符串

问题描述:

多维数组我想从下面生成一个PHP多维数组 -生成列表字符串

- Item 1 
-- Item 1.1 
-- Item 1.2 
--- Item 1.2.1 
--- Item 1.2.2 
---- Item 1.2.2.1 
- Item 2 
- Item 3 
- Item 4 
-- Item 4.1 
-- Item 4.2 

我的最终目标是这个字符串转换成无序列表。

我想象最好的方法来做到这一点将创建一个递归函数。在一个美好的一天,我确信我可以解决这个问题,但我有点空虚!

阵列结构应该像下面转储 -

array(6) { 
    [0]=> 
    string(6) "Item 1" 
    [1]=> 
    array(3) { 
    [0]=> 
    string(8) "Item 1.1" 
    [1]=> 
    string(8) "Item 1.2" 
    [2]=> 
    array(3) { 
     [0]=> 
     string(10) "Item 1.2.1" 
     [1]=> 
     string(10) "Item 1.2.2" 
     [2]=> 
     array(1) { 
     [0]=> 
     string(12) "Item 1.2.2.1" 
     } 
    } 
    } 
    [2]=> 
    string(6) "Item 2" 
    [3]=> 
    string(6) "Item 3" 
    [4]=> 
    string(6) "Item 4" 
    [5]=> 
    array(2) { 
    [0]=> 
    string(8) "Item 4.1" 
    [1]=> 
    string(8) "Item 4.2" 
    } 
} 

希望能对你有所帮助。

+0

https://www.google.com/search?q=build+multidimensional+array+from+string+recursion //大部分结果似乎是针对PHP的,但由于您甚至没有提及或标记具体的语言,那我必须这样做。 – CBroe

+0

@CBroe感谢您的Google链接。这是PHP - 对不起,我忘了包括现在添加它。这比你的标准字符串递归复杂一点 – Chris

+0

从上面的注释中,你可以给出一个提示数组应该是什么样子? –

下面将转换直接进入HTML,而不递归:

$text = array(); 
$text[] = '- Item 1'; 
$text[] = '-- Item 1.1'; 
$text[] = '-- Item 1.2'; 
$text[] = '--- Item 1.2.1'; 
$text[] = '--- Item 1.2.2'; 
$text[] = '---- Item 1.2.2.1'; 
$text[] = '- Item 2'; 
$text[] = '- Item 3'; 
$text[] = '- Item 4'; 
$text[] = '-- Item 4.1'; 
$text[] = '-- Item 4.2'; 

$previous_dash_count = 0; // topmost parent 
foreach ($text as $line) { 
    if (preg_match('/(^\-+)(.*)/', $line, $matches, PREG_OFFSET_CAPTURE)===1) { 
     $dash_count = strlen($matches[1][0]); 
     $title = $matches[2][0]; 

     if ($dash_count == $previous_dash_count) { 
      echo "<li>$title</li>\n"; 
     } elseif ($dash_count > $previous_dash_count) { 
      echo str_repeat("<ul>\n", $dash_count - $previous_dash_count); 
      echo "<li>$title</li>\n"; 
     } else { 
      echo str_repeat("</ul>\n",$previous_dash_count-$dash_count+1); 
      echo "<ul>\n"; 
      echo "<li>$title</li>\n"; 
     } 

     $previous_dash_count = $dash_count; 
    } 
} 
echo str_repeat("</ul>\n",$previous_dash_count); 

我做几个假设。输入文本总是表现良好并且不包含随机性。另外我不会假设UTF-8文本,但是您可以安全地使用破折号。

这里是在其所有的荣耀血腥数组版本:

$stack = array(); 
$previous_dash_count = 0; 
$parent_node = array(); 
foreach ($text as $line) { 
    if (preg_match('/(^\-+)(.*)/', $line, $matches, PREG_OFFSET_CAPTURE)===1) { 
     $dash_count = strlen($matches[1][0]); 
     $title = $matches[2][0]; 

     if ($dash_count == $previous_dash_count) { 
      $parent_node[] = $title; 
     } elseif ($dash_count > $previous_dash_count) { 
      for ($push_count = $previous_dash_count; $push_count<$dash_count; $push_count++) { 
       array_push($stack, $parent_node); // remember node 
       $new_child = array(); 
       $new_child[] = $title; 
       $parent_node[] = $new_child; 
       $parent_node = $new_child; 
      } 
     } else { 
      for ($pop_count = $previous_dash_count; $pop_count >$dash_count; $pop_count--) { 
       $old_child = $parent_node; 
       $parent_node = array_pop($stack); 
       $parent_node[] = $old_child; 
      } 
      $parent_node[] = $title; 
     } 

     $previous_dash_count = $dash_count; 
    } 
} 
for ($pop_count = $previous_dash_count; $pop_count > 0; $pop_count--) { 
    $old_child = $parent_node; 
    $parent_node = array_pop($stack); 
    $parent_node[] = $old_child; 
} 

print_r($parent_node); 

我们保持阵列节点的堆栈,所以我们有一个孩子与其父之间的联系。请注意,此代码的结构与直接HTML版本的结构相同。

+0

谢谢 - 我最终不得不采用这种方法,因为它是最简单的。稍微不同的技巧,但你的答案是正确的。干杯! – Chris

+0

太棒了,非常感谢你! Cudos :) – Chris

+0

很高兴为您提供帮助。这比我想象的要难,它让我流汗... –