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

  • SEARCH
  • Home
  • 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 8010623
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T18:47:42+00:00 2026-06-04T18:47:42+00:00

My question is: I have a mysql table: events and would like to get

  • 0

My question is:
I have a mysql table: events and would like to get special formatted json_string.

    id   event
    1    2010-01-01 00:00:00 
    2    2010-07-02 00:00:00 
    3    2011-01-04 00:00:00 
    4    2012-01-30 00:00:00 
    5    2012-03-15 00:00:00 
...

I need to get json string:

{"events":[
   {"2010":
      {
       "1":{"id":1,"event":2010-01-01 00:00:00},
       "7":{"id":2,"event":2010-07-02 00:00:00}
      },
   },
   {"2011:
      {
       "1":{"id":3,"event":2012-01-30 00:00:00}
      },
   },
   {"2012:
      {
       "1":{"id":4,"event":2011-01-04 00:00:00},
       "3":{"id":5,"event":2012-03-15 00:00:00},
      },
   }
]}

My code below:

$result = DB::query('SELECT id, event FROM events');
 $events = array();
 while($event = $result->fetch_object()) {
     // the following statement won't get desired results
     $events[] = $event;
 }

 return array(
    'events' => json_encode($events);
 ); 

How do I change my code to get the above JSON string?

  • 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-06-04T18:47:44+00:00Added an answer on June 4, 2026 at 6:47 pm

    Just substr() the parts you need from the event date and use that to construct a multi-dimensional array:

    while($event = $result->fetch_object()) {
        $year = substr($event->event, 0, 4);
        $month = (int)substr($event->event, 5, 2);
    
        $events[$year][$month][] = $event;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a question regarding an SQL syntax. I have a mysql table like
Suppose I have a MySQL table called MyTable, that looks like this: +----+------+-------+ |
I have another little question. I have a mysql-table cat | item | data
Apologies in advance if this question is badly worded! I have a MySQL table
I have a question about MySQL InnoDB. For example: I have the following table
I have 2 question I wanna make Mysql table that have charset utf8 but
My question: I have a mysql database that consists of something like a fact
Let's assume we have MySQL table events from which we'll select a data. Now
I have the following MySql table containing my raw event data (about 1.5 million
I have 2 mysql tables : Question with the following columns : id, question,

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.