[英]php code to output three level navigation
I'm working on an ExpressionEngine site and I have a loop outputting a sorted list. 我在ExpressionEngine网站上工作,并且有一个循环输出排序列表。 What I would like to do is output a list three levels deep using PHP.
我想做的是使用PHP输出三个层次的列表。
/apples/black/rotten
/apples/green/cortland
/apples/green/cortland
/apples/red/granny
/apples/red/granny
/bananas/green/bad
/bananas/green/bad
/bananas/yellow/good
/bananas/yellow/good
/oranges/orange/tasty
/oranges/yellow/small
/oranges/yellow/small
What is the best method for converting this to a nested list? 将其转换为嵌套列表的最佳方法是什么?
Obviously this is a made up list for illustration. 显然,这是一个用于说明的虚构清单。 I've been trying to figure this out for a couple of days now and have had no luck.
我已经尝试了几天了,并且没有运气。 Is recursion an option.
递归是一个选项。
$arr = array();
foreach (file(..., FILE_IGNORE_NEW_LINES) as $line)
{
$tmp = explode("/", substr($line, 1));
for ($i=0; $i<=2; $i++)
{
switch ($i)
{
case 0:
if ( ! isset($arr[$tmp[0]]))
{
$arr[$tmp[0]] = array();
}
break;
case 1:
if ( ! isset($arr[$tmp[1]]))
{
$arr[$tmp[0]][$tmp[1]] = $tmp[2];
}
break;
}
}
}
Use of file is assuming the expression results is stored into a text file, 使用file假设表达式结果存储在文本文件中,
you can directly iterate if is already in array 您可以直接迭代是否已经在数组中
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.