0

我正在创建一个显示年份和问题编号的递归菜单。不知何故,它只是为每个数组元素打印一次。

这就是我需要的: 请选择一年 -> 2013 -> Issue#1, Issue# 2, Issue# 3, Issue# 4

我得到的是: 请选择一年 -> 2013 ->问题#1

到目前为止,这是我的代码:

<?php
echo "<div id='cssmenu'>";
echo "<ul>";
echo "<li class='has-sub'><a href='#'><span>Please select a Year</span></a>";
echo "<ul>";
$dir = new DirectoryIterator('/usr/apps/webdata/backend/assets/newsletters/temp');
$list = array();
foreach($dir as $file)
{
    if ($file->isDot()) {
        continue;
    }

    $string = $file;
    if (preg_match_all("#(\d{1,})#", $string, $matches, PREG_SET_ORDER)) {
        foreach($matches as $match)
        {
            $issue = $match[0];

            while (list($key, $value) = each($match)) {
                //YEAR
                if (strlen($value) == 4) {
                    $uyear = $value;
                    //echo "Year: $uyear \n";
                }
                // ISSUE#
                elseif (strlen($value) == 1) {
                    $uissue = $value;
                    //echo "Issue: $uissue \n";
                }
            }
        }
    }

    if (!isset($list[$uyear])) {
        $list[$uyear] = array();
        echo "<li class='has-sub'><a href='#'><span>" . $uyear . " </span></a>";
        echo "<ul>";
    }

    if (!in_array($uissue, $list[$uyear])) {
        $list[$uyear][] = $uissue;
        $a = count($uissue);
        for ($i=0; $i<$a; $i++) {
            echo " <a class=fancypdf href='#' onclick=myPDF('".$file."')> Issue# ".$uissue."   </a>";
            echo "</li>";
        }
        echo "</ul>";
        echo "</li>";
    }
}

echo "</ul>";
echo "</ul>";
echo "</ul>";
echo "</div>";
////IM USING THIS PRINT JUST TO VERIFY IF THE FUNCTIONS GOES THROUGH ALL THE ELEMENTS
print_r($list);
?>

更新 我能够找到问题;我的递归函数不正确,这就是它只打印第一个元素的原因。但是,我不知道如何实际打印第二个元素。

$a = count($uissue);
for($i=0;$i<$a; $i++)
{
    echo " <a class=fancypdf href='#' onclick=myPDF('".$file."')> Issue# ".$uissue."   </a>";
    echo "</li>";
}

可能使用 For-Each 函数?我仍在努力解决这个问题。

4

0 回答 0