phparraysmultidimensional-array

How to restructure an indexed 2d array to be an associative 2d array as well as remove unneeded columns and rename columns?


I'm struggling building up a multidimesional array from database to display data in a calendar... Basically, I have a table, where there always is a date, a description and a link to the event. I then want to display that data in a calendar and found a little php programmed one, that looks okay.

Now this PHP code takes a $events variable to display events, the structure of the array has to be the following:

$events = [
  '2020-04-05' => [
    'text' => "An event for the 5 july 2015",
    'href' => "http://example.com/link/to/event"
  ],
  '2020-04-23' => [
    'text' => "An event for the 23 july 2015",
    'href' => "/path/to/event"
  ],
];

Now as I didn't work too much with multidimensional arrays in PHP (except the ones received as database result), I tried building this array up, but I can't seem to be able to..

What I get from my db is an array, that looks like the following:

array (


 0 => 
  array (
    'ID' => '1',
    'link' => 'example.com/link',
    'description' => 'that\'s a description',
    'eventDate' => '2020-04-07 01:04:25',
  ),
  1 => 
  array (
    'ID' => '6',
    'link' => 'example.com/link',
    'description' => 'that\'s a description',
    'eventDate' => '2020-04-15 00:00:00',
  ),
  2 => 
  array (
    'ID' => '7',
    'link' => 'example.com/link',
    'description' => 'that\'s a description',
    'eventDate' => '2020-04-16 07:24:11',
  ),
  3 => 
  array (
    'ID' => '8',
    'link' => 'example.com/link',
    'description' => 'that\'s a description',
    'eventDate' => '2020-04-26 07:07:10',
  ),
)

So I thought of a foreach over that array, create a new array for the events before and build it up by something like $events[$foreachVar["date"] = "test" in order to at least get that index, but not even that helps..

Can anybody help me out with that or is there a better way to do it (a better calendar, that integrates the database better?)

PS: If it minds, I'm using Fat Free Framework as a little microframework.


Solution

  • You can iterate over your database result, getting the date portion of eventDate as the output array key and selecting the description and link elements for the output:

    $events = array();
    foreach ($data as $event) {
        $events[date('Y-m-d', strtotime($event['eventDate']))] = array(
            'text' => $event['description'],
            'href' => $event['link']
        );
    }
    var_export($events);
    

    Output (for your sample data):

    array (
      '2020-04-07' => 
      array (
        'text' => 'that\'s a description',
        'href' => 'example.com/link',
      ),
      '2020-04-15' => 
      array (
        'text' => 'that\'s a description',
        'href' => 'example.com/link',
      ),
      '2020-04-16' => 
      array (
        'text' => 'that\'s a description',
        'href' => 'example.com/link',
      ),
      '2020-04-26' => 
      array (
        'text' => 'that\'s a description',
        'href' => 'example.com/link',
      ),
    )
    

    Demo on 3v4l.org

    Note I've been a bit paranoid with the date, wanting to allow for any format in the database. If it's definitely in yyyy-mm-dd HH:ii:ss form, you can just use substr($event['eventDate'], 0, 10) to extract the date portion i.e.

    foreach ($data as $event) {
        $events[substr($event['eventDate'], 0, 10)] = array(
            'text' => $event['description'],
            'href' => $event['link']
        );
    }