代码:
// Mmmm... functiony goodness
function array_to_toc ($in, &$out, $level = '') {
if (!$level) $out = array(); // Make sure $out is an empty array at the beginning
foreach ($in as $key => $item) { // Loop items
$thisLevel = ($level) ? "$level.".($key + 1) : ($key + 1); // Get this level as string
$out[$thisLevel] = $item['name']; // Add this item to $out
if (isset($item['subs']) && is_array($item['subs']) && count($item['subs'])) array_to_toc($item['subs'],$out,$thisLevel); // Recurse children of this item
}
}
// Here is your test data (slightly modified - I think you stated it wrong in the question)
$array = array (
0 => array (
'name' => 'test1',
'subs' => array (
0 => array (
'name' => 'test2'
),
1 => array (
'name' => 'test3',
'subs' => array (
0 => array (
'name' => 'test4'
)
)
)
)
),
1 => array (
'name' => 'test5'
)
);
// $result is passed by reference and will hold the output after the function has run
$result = array();
array_to_toc($array, $result);
print_r($result);
输出:
Array
(
[1] => test1
[1.1] => test2
[1.2] => test3
[1.2.1] => test4
[2] => test5
)
Demo
编辑
这两个(加上一个支持)函数允许您通过章节引用从输入数组中添加和删除章节。然后,您可以根据新结构重新计算 TOC。
function chapter_exists ($array, $chapterId) {
$chapterParts = explode('.',$chapterId);
foreach ($chapterParts as &$chapter) $chapter--;
$lastId = array_pop($chapterParts);
return eval('return isset($array['.implode("]['subs'][",$chapterParts).((count($chapterParts)) ? "]['subs'][" : '')."$lastId]);");
}
function add_chapter (&$array, $chapterId, $item) {
$chapterParts = explode('.',$chapterId);
foreach ($chapterParts as &$chapter) $chapter--; // Decrement all the values
$lastId = array_pop($chapterParts);
if (count($chapterParts) && !chapter_exists($array, implode('.',$chapterParts))) return FALSE; // Return FALSE if the level above the chapter we are adding doesn't exist
if (chapter_exists($array, $chapterId)) { // See if the chapter reference already exists
eval('array_splice($array'.((count($chapterParts)) ? '['.implode("]['subs'][",$chapterParts)."]['subs']" : '').",$lastId,0,array(\$item));"); // Insert an item
} else {
eval('$array['.implode("]['subs'][",$chapterParts).((count($chapterParts)) ? "]['subs'][" : '')."$lastId] = \$item;"); // Insert an item
}
return TRUE;
}
function remove_chapter (&$array, $chapterId) {
$chapterParts = explode('.',$chapterId);
foreach ($chapterParts as &$chapter) $chapter--; // Decrement all the values
$lastId = array_pop($chapterParts);
return (chapter_exists($array, $chapterId)) ? eval('$removed = array_splice($array'.((count($chapterParts)) ? '['.implode("]['subs'][",$chapterParts)."]['subs']" : '').",$lastId,1); return array_shift(\$removed);") : FALSE;
}
演示它们如何工作的最佳方式是通过示例。假设我们从上面的数组结构开始,它保存在一个名为$structure 的变量中。我们知道,我们生成的 TOC 数组如下所示:
Array
(
[1] => test1
[1.1] => test2
[1.2] => test3
[1.2.1] => test4
[2] => test5
)
现在,我们决定要删除章节 1.2 及其所有子章节 - 我们可以这样做:
// Remove the chapter from $structure
remove_chapter($structure, '1.2');
// recalculate the TOC
array_to_toc($structure, $result2);
print_r($result2);
/*
Outputs:
Array
(
[1] => test1
[1.1] => test2
[2] => test5
)
*/
现在假设我们要添加一个名为 test6 的章节作为章节 1.1,并且 test2 将被重新索引为 1.2 - 我们将使用上述示例的结果一:
// Add the new chapter to $structure
add_chapter($structure, '1.1', array('name'=>'test6'));
// recalculate the TOC
array_to_toc($structure, $result3);
print_r($result3);
/*
Outputs:
Array
(
[1] => test1
[1.1] => test6
[1.2] => test2
[2] => test5
)
*/
好的,看起来很简单。但是如果我们想移动一个子章节,让它在树的顶层呢?让我们回到我们原来的 $structure 版本来证明这一点 - 我们将移动章节 1.2,所以它现在是章节 3:
/*
A quick reminder of what we are starting with:
Array
(
[1] => test1
[1.1] => test2
[1.2] => test3
[1.2.1] => test4
[2] => test5
)
*/
// Remove the chapter from $structure - this time, we'll catch the items we remove in a variable
$removed = remove_chapter($structure, '1.2');
// Add it again, only this time as chapter 3
add_chapter($structure, '3', $removed);
// recalculate the TOC
array_to_toc($structure, $result4);
print_r($result4);
/*
Outputs:
Array
(
[1] => test1
[1.1] => test2
[2] => test5
[3] => test3
[3.1] => test4
)
*/
希望我已经解释得足够好了。
chapter_exists() 返回一个布尔值。如果感觉的话,它的含义相当不言自明。将$structure 数组作为第一个参数传递,您要检查的章节ID 作为第二个参数。这个函数是必需的,因为它被其他两个内部使用。
add_chapter() 返回一个布尔值,因此您可以测试操作是否成功。如果章节的父级不存在,它将失败 - 例如,如果您尝试在 1.2 尚未定义时添加 1.2.1,它将不起作用。如果添加已经存在的章节,则该级别的所有章节编号都会上移 1。
remove_chapter() 将返回成功时删除的项目(即数组)或布尔值 FALSE 失败 - 如果您尝试删除不存在的章节,它将失败。
注意:为了适应任意关卡深度,我不得不大量使用eval()。我讨厌使用它,但我想不出任何其他方式 - 如果有人阅读本文对替代方法有任何好的想法(最好不涉及一些噩梦般的循环结构),请告诉我......