我有以下PHP数组。
Array
(
[168] => Array
(
[link] => asdfasdf
[children] => Array
(
[239] => Array
(
[link] => tascatestlalal
[children] => Array
(
)
)
[240] => Array
(
[link] => otrotestttt
[children] => Array
(
)
)
)
)
[229] => Array
(
[link] => Sub-task ex
[children] => Array
(
)
)
[230] => Array
(
[link] => Sub-task test
[children] => Array
(
[231] => Array
(
[link] => tasktest1
[children] => Array
(
)
)
[232] => Array
(
[link] => tasktest 2
[children] => Array
(
[233] => Array
(
[link] => tasktest 5
[children] => Array
(
[235] => Array
(
[link] => tasca235
[children] => Array
(
)
)
)
)
)
)
[234] => Array
(
[link] => tasca234
[children] => Array
(
)
)
)
)
)
我需要将其转换为此
<table>
<tr>
<td>
Sub-task
</td>
</tr>
<tr>
<td>
--Tasktest1
</td>
</tr>
<tr>
<td>
--tasktest 2
</td>
</tr>
<tr>
<td>
---tasktest 5
</td>
</tr>
<tr>
<td>
----tasca235
</td>
</tr>
<tr>
<td>
--tasca234
</td>
</tr>
</table>
这个我已经知道如何用这个函数的列表来做,但是看不到修改这个函数将它转换成上面的表示例:(。一些帮助将不胜感激
function ArrayToHTMLList($arr) {
$str = "<ul class='tasklist'>";
foreach($arr as $key => $value) {
if((!empty($value))){
$str .="<li>";
if(is_array($value))
$str .= ArrayToHTMLList($value);
else
$str .= $value;
$str .= "</li>";
}
}
$str .= "</ul>";
return $str;
}
答案 0 :(得分:1)
function nestedArrayToFlatList($node, $indent) {
$str = '';
if (isset($node['link'])) {
$str .= '<tr><td>' . $indent . $node['link'] . '</td></tr>';
}
if (isset($node['children']) && count($node['children']) > 0) {
foreach ($node['children'] as $child) {
$str .= nestedArrayToFlatList($child, '--' . $indent);
}
}
return $str;
}
$myList = '<table>' . nestedArrayToFlatList($myMassiveArray, '') . '</table>';