dongzhi6087 2013-02-13 14:51
浏览 9
已采纳

嵌套数组到HTML存档。 包含示例代码

I've generated an array of dates. Here is a quick sample

Array
(
[2013] => Array
    (
        [Feb] => 2013
        [Jan] => 2013
    )

[2012] => Array
    (
        [Oct] => 2012
        [Jun] => 2012
        [May] => 2012
    )

)

Sample code:

<?php
$posts = get_posts(array('numberposts' => -1, 'post_status' => 'publish', 'post_type' => 'post', ));
#       $i=1;
print '<pre>';
foreach ($posts as $post) {
    #       $post_results[$post->ID][month] = get_the_time('m', $post->ID);
    #       $post_results[$post->ID][year]  = get_the_time('Y', $post->ID);
    $year = get_the_time('Y', $post -> ID);
    $month = get_the_time('M', $post -> ID);
    $post_results[$year][$month] = get_the_time('Y', $post -> ID);
    #       echo "$i. Post ID: "  .$post->ID." - " .get_the_time('Y-m-d', $post->ID)."<br/>";
    #       $i++;
}

foreach ($post_results as $key => $value) {
    echo "Month: " . $key . " Year: " . $value . "<br/>";
}

print_r($post_results);
print '</pre>';
?>

I'd like to list all dates in the same format as this webpage http://www.smartpassiveincome.com/archives/

What I need is help getting my array to separate values like what you see above. I can do the linking part myself, I just need the data pulled into easy to use variables for me.

For example, I'd like to say "For each year, get all the months listed, then type out <td>$year</td><td>$month</td> then move on to the previous year.

  • 写回答

3条回答 默认 最新

  • dougong5817 2013-02-13 15:12
    关注

    Here's what I think you need, his is one way to achieve the following:

    2013 | Feb | Jan
    2012 | Oct | Jun | May

    Original:

    $arrYears = array(2013=>array("Feb"=>2013, "Jan"=>2013), 2012=>array("Oct"=>2013, "Jun"=>2013, "May"=>2013));
            foreach($arrYears as $strYear=>$arrMonths) {
                echo $strYear;
                // This is our logic for each year
                foreach($arrMonths as $strMonth=>$strYear2) {
                    echo " | ".$strMonth;
                }
                echo "<br />";
            }
    

    Edit: Put months in a nested list

     $arrYears = array(
                     2013=>array("Feb"=>2013, "Jan"=>2013),
                     2012=>array("Oct"=>2013, "Jun"=>2013, "May"=>2013)
                  );
    
     echo "<ul>";
         foreach($arrYears as $strYear=>$arrMonths) {
             echo "<li>";
                 echo $strYear;
                 echo "<ul>";
    
                    foreach($arrMonths as $strMonth=>$strYear2) {
                        echo "<li><a href='#'>".$strMonth."</a></li>";
                    }
    
                 echo "</ul>";
             echo "</li>";
         }
     echo "</ul>";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端