为什么将深度为 1 的行添加到数组 [1]
Why are lines with depth of 1 added to array[1]
我正在使用以下函数将选项卡式列表转换为数组,但深度为 1 的选项卡被添加到数组[1];
选项卡列表:
$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)";
函数:
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 );
}
}
这是运行"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
)
那么为什么我得到数组[1][6]而不是数组[6],数组[5]之后的所有行依此类推?
我认为如果您 re-design 如何存储数据应该会更容易。我的建议是为每个级别创建一个数组,其中包含标签的键和子级别的另一个键。
$test = "Test 1\n
\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\n
\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";
然后它应该看起来像这样:
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()
)
)
)
)
)
)
我正在使用以下函数将选项卡式列表转换为数组,但深度为 1 的选项卡被添加到数组[1];
选项卡列表:
$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)";
函数:
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 );
}
}
这是运行"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
)
那么为什么我得到数组[1][6]而不是数组[6],数组[5]之后的所有行依此类推?
我认为如果您 re-design 如何存储数据应该会更容易。我的建议是为每个级别创建一个数组,其中包含标签的键和子级别的另一个键。
$test = "Test 1\n
\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\n
\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";
然后它应该看起来像这样:
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()
)
)
)
)
)
)