Skip to content

Instantly share code, notes, and snippets.

@Dinir
Created October 6, 2017 06:51
Show Gist options
  • Save Dinir/b15c8b13fbfa03165ad7b173e40936c6 to your computer and use it in GitHub Desktop.
Save Dinir/b15c8b13fbfa03165ad7b173e40936c6 to your computer and use it in GitHub Desktop.
the purpose is too specific I won't add a detailed explanation here
<?php
// temporary explanation:
// $menu_href is a multi-dimensional array with depth levels for each item as values
// $menu_name is a multi-dimensional array with names for each item as values
// Yes these are not so effective ways to contain list datas. Terrible, actually.
// @@ gets the link, ## gets the content, $$ gets the contents under it (if there is)
$sub_menu_format = array(
array(
'',
"<li class='sub'>\n"."$$\n"."</li>\n",
''
),
array(
"<ul class='menu'>\n",
"<li>\n"."<a href='inner.php?sMenu=@@'>\n##</a>\n"."$$"."</li>\n",
"</ul>"
),
array(
"<ul>\n",
"<li>\n"."<a href='inner.php?sMenu=@@'>\n##</a>\n"."$$"."</li>\n",
"</ul>\n"
),
array(
"<ul>\n",
"<li>\n"."<a href='inner.php?sMenu=@@'>\n##</a>\n"."$$"."</li>\n",
"</ul>"
)
);
$sub_menu = function(
$menu_href, $menu_name, $format, $content_indicator
) {
$attempt = 0;
reset($menu_href);
reset($menu_name);
$make = function(&$menu_href, &$menu_name, $level = 0) use (
&$make, &$attempt,
$format, &$content_indicator
) {
reset($menu_href);
reset($menu_name);
$html = '';
$html .= str_repeat("\t", $level).$format[$level][0];
while(key($menu_name) !== null && $attempt <= 50) {
$attempt++;
$last_line = str_repeat("\t", $level).$format[$level][1];
if(is_array(current($menu_name))) {
$sub_menu_href = current($menu_href);
$sub_menu_name = current($menu_name);
$last_line = str_replace(
$content_indicator,
array(
key($menu_href),
key($menu_name),
$make($sub_menu_href, $sub_menu_name, $level + 1)
),
$last_line
);
} else {
$last_line = str_replace(
array($content_indicator[0], $content_indicator[1]),
array(
current($menu_href),
current($menu_name)."\n"
),
$last_line
);
}
$last_line = str_replace($content_indicator, '', $last_line);
$html .= $last_line;
next($menu_href);
next($menu_name);
}
$html .= str_repeat("\t", $level).$format[$level][2];
return $html;
};
return $make($menu_href, $menu_name);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment