Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7696913
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T21:53:26+00:00 2026-05-31T21:53:26+00:00

I am looking to create a list in the following format: 2012 (2) –

  • 0

I am looking to create a list in the following format:

2012 (2) 
- January (1) 
- March (1) 
2011 (1) 
- March (1)

from am array of dates in the following format:

Array
(
    [0] => Array
        (
            [year] => 2012
            [month] => 3
        )

    [1] => Array
        (
            [year] => 2012
            [month] => 1
        )

    [2] => Array
        (
            [year] => 2011
            [month] => 3
        )

)

this list is provided from the following query, and i’m open to suggestions as how to return the data as well. This was the most logical step for me.

SELECT YEAR(post_date) as `year`, MONTH(post_date) as `month` FROM posts ORDER BY post_date DESC

I’m at a dead end here. I don’t know if I’m not seeing something obvious or if I’ve made this too complicated but I can’t figure out where to go from here.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T21:53:28+00:00Added an answer on May 31, 2026 at 9:53 pm

    Assuming you might have multiple rows with the same month/year:

    # first sort into a 2-level array of post counts
    $by_year = array();
    $year_counts = array();
    foreach ($rows as $row){
        $by_year[$row['year']][$row['month']]++;
        $year_counts[$row['year']]++;
    }
    
    # most recent year first!
    krsort($by_year);
    
    # and now iterate for display
    foreach ($by_year as $year => $months){
    
        echo "<b>$year</b> ($year_counts[$year])<br />\n";
        echo "<ul>\n";
    
        # sort jan->dec
        ksort($months);
        foreach ($months as $month => $num){
    
            $name = date('F', mktime(0,0,0,$month,1,2010));
            echo "<li> $name ($num) </li>\n";
        }
    
        echo "</ul>\n";
    }
    

    This works as you requested, with this input data:

    $rows = array(
        array('year' => 2012, 'month' => 3),
        array('year' => 2012, 'month' => 3),
        array('year' => 2012, 'month' => 2),
        array('year' => 2011, 'month' => 1),
    );
    

    It outputs this:

    2012 (3)
     * February (1)
     * March (2)
    2011 (1)
     * January (1)
    

    *updated to show year counts too

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am looking for a convenient way to create a list of lists for
I'm looking to create favicon.ico files programatically from Python, but PIL only has support
I am looking to create symlinks (soft links) from Java on a Windows Vista/
I'm looking for possibility to define following options for Sharepoint list. I know it
I'm looking to create a similar combobox or dropdown list like this where the
I'm looking to create a display similar to the following screenshot: The main point
I've been looking for the follow solution to create the array structure I need.
I'm looking to create an SDL Tridion schema with a list of repeatable links
I looking to create a custom calender with Zend Framework, I am hoping that
Im looking to create a control that would look like comic baloon. In WPF

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.