PHP - 根据字符串的分隔符创建多维数组
PHP - create a multidimensional array according to a string's delimiters
我想拆分以下文本:
"some text [aaa[b[c1][c2]d]e][test] more text"
进入嵌套数组:
Array
(
[0] => aaa[b[c1][c2]d]e
[1] => Array
(
[0] => b[c1][c2]d
[1] => Array
(
[0] => c1
[1] => c2
)
)
[2] => test
)
php 手册中的 "recursiveSplit" 函数可以很好地显示结果。
函数如下:
function recursiveSplit($string, $layer) {
preg_match_all("#\[(([^\[\]]*|(?R))*)\]#", $string, $matches);
// iterate thru matches and continue recursive split
if (count($matches) > 1) {
for ($i = 0; $i < count($matches[1]); $i++) {
if (is_string($matches[1][$i])) {
if (strlen($matches[1][$i]) > 0) {
echo "<pre>Layer ".$layer.": ".$matches[1][$i]."</pre><br />";
recursiveSplit($matches[1][$i], $layer + 1);
}
}
}
}
}
recursiveSplit($string, 0);
显示如下:
Layer 0: aaa[b[c1][c2]d]e
Layer 1: b[c1][c2]d
Layer 2: c1
Layer 2: c2
Layer 0: test
我无法修改函数以将结果放入数组甚至简单的字符串中。我完全卡住了。有什么想法吗?
你只需要将项目添加到结果数组而不是回显,并且还添加 recursiveSplit 调用结果,就像这样:
<?php
function recursiveSplit($string, $layer) {
$result = array();
preg_match_all("#\[(([^\[\]]*|(?R))*)\]#", $string, $matches);
// iterate thru matches and continue recursive split
if (count($matches) > 1) {
for ($i = 0; $i < count($matches[1]); $i++) {
if (is_string($matches[1][$i])) {
if (strlen($matches[1][$i]) > 0) {
$result[] = $matches[1][$i];
echo "<pre>Layer ".$layer.": ".$matches[1][$i]."</pre><br />";
$rec = recursiveSplit($matches[1][$i], $layer + 1);
if ($rec) {
$result[] = $rec;
}
}
}
}
}
return $result;
}
$string = "some text [aaa[b[c1][c2]d]e][test] more text";
$result = recursiveSplit($string, 0);
print_r($result);
我想拆分以下文本:
"some text [aaa[b[c1][c2]d]e][test] more text"
进入嵌套数组:
Array
(
[0] => aaa[b[c1][c2]d]e
[1] => Array
(
[0] => b[c1][c2]d
[1] => Array
(
[0] => c1
[1] => c2
)
)
[2] => test
)
php 手册中的 "recursiveSplit" 函数可以很好地显示结果。
函数如下:
function recursiveSplit($string, $layer) {
preg_match_all("#\[(([^\[\]]*|(?R))*)\]#", $string, $matches);
// iterate thru matches and continue recursive split
if (count($matches) > 1) {
for ($i = 0; $i < count($matches[1]); $i++) {
if (is_string($matches[1][$i])) {
if (strlen($matches[1][$i]) > 0) {
echo "<pre>Layer ".$layer.": ".$matches[1][$i]."</pre><br />";
recursiveSplit($matches[1][$i], $layer + 1);
}
}
}
}
}
recursiveSplit($string, 0);
显示如下:
Layer 0: aaa[b[c1][c2]d]e
Layer 1: b[c1][c2]d
Layer 2: c1
Layer 2: c2
Layer 0: test
我无法修改函数以将结果放入数组甚至简单的字符串中。我完全卡住了。有什么想法吗?
你只需要将项目添加到结果数组而不是回显,并且还添加 recursiveSplit 调用结果,就像这样:
<?php
function recursiveSplit($string, $layer) {
$result = array();
preg_match_all("#\[(([^\[\]]*|(?R))*)\]#", $string, $matches);
// iterate thru matches and continue recursive split
if (count($matches) > 1) {
for ($i = 0; $i < count($matches[1]); $i++) {
if (is_string($matches[1][$i])) {
if (strlen($matches[1][$i]) > 0) {
$result[] = $matches[1][$i];
echo "<pre>Layer ".$layer.": ".$matches[1][$i]."</pre><br />";
$rec = recursiveSplit($matches[1][$i], $layer + 1);
if ($rec) {
$result[] = $rec;
}
}
}
}
}
return $result;
}
$string = "some text [aaa[b[c1][c2]d]e][test] more text";
$result = recursiveSplit($string, 0);
print_r($result);