Custom archives page by month and year – nesting problem

After much more work, I developed a solution that outputs archives by year, month and day while minimising database calls. Posting it here for others to use:

//* Add custom archives below entry content
add_action( 'genesis_entry_content', 'custom_archives_page_content' );
function custom_archives_page_content() {

global $post;

// set post arguments
$args = array(
    'posts_per_page'    => -1,
    'orderby'       => 'post_date',
    'order'         => 'DESC',
    'post_type'     => 'post',
    'post_status'       => 'publish',
);

// get posts from wordpress
$posts = get_posts($args);

// setup an associative array with three levels, for every year, month and posts
$years = array();

foreach ($posts as $post) {

    $year  = mysql2date('Y', $post->post_date);
    $month = mysql2date('F', $post->post_date);

    // specify the position of the current post
    $years[$year][$month][] = $post;
}

// begin the archives ?>

<ul class="yearly-archives">

<?php foreach ($years as $year => $months) : // shows the years ?>

    <li><h2><?php echo $year ?></h2><ul class="monthly-archives">

    <?php foreach ($months as $month => $posts ) : // shows the months ?>

        <li><h3><?php printf("%s (%d)", $month, count($months[$month])) ?></h3><ul class="daily-archives">

        <?php foreach($posts as $post) : setup_postdata($post); // shows the posts ?>

            <li>
            <span class="date"><?php the_time('d') ?>:</span>
            <a href="https://wordpress.stackexchange.com/questions/203813/<?php the_permalink(); ?>"><?php the_title(); ?></a>
            <span class="comments-number"><?php comments_number('(0)','(1)','(%)'); ?></span>
            </li>

        <?php endforeach; // ends foreach for $posts ?>
        </ul><!-- /daily-archives --></li>

    <?php endforeach; // ends foreach for $months ?>
    </ul><!-- /monthly-archives --></li> 

<?php endforeach; // ends foreach for $years ?>
</ul><!-- /yearly-archives -->
<?php
}

Props to davidmh and mikos.