Convert Array to a simpler Array

How can I convert an array #EVENTS_ARRAY to look like #CALENDAR_ARRAY.
The keys must be the day of the month.
The key values can be empty.
The CI calendar library requires this simple array and I want to provide all of the summarized info in the value for customization reasons.

Basically the #CALENDAR_ARRAY contains the same data but is summarized and includes a count of how many events occur on one day.

If the counting events for each day is too complicated can it be done in the mysql statement and possibly be included in the first array?


# EVENTS ARRAY #
Array
(
    [0] => Array
        (
            [uid] => 111
            [birthday_date] => 1958-06-02
            [name] => Name 1
        )

    [1] => Array
        (
            [uid] => 222
            [birthday_date] => 1958-06-03
            [name] => Name 2
        )

    [2] => Array
        (
            [uid] => 333
            [birthday_date] => 1958-06-03
            [name] => Name 3
        )

    [3] => Array
        (
            [uid] => 444
            [birthday_date] => 1958-06-05
            [name] => Name 4
        )
)



# CALENDAR ARRAY #
Array
(
[1] => 
[2] => 1 entry,   Name 1, 111
[3] => 2 entries, Name 2, 222 - Name 3, 333
[4] => 
[5] => 1 entry,   Name 4, 444
)

The following is also acceptable.

Array
(
[2] => 1 entry,   Name 1, 111
[3] => 2 entries, Name 2, 222 - Name 3, 333
[5] => 1 entry,   Name 4, 444
)




Clearly the work of a genius! Thank you sir. I made a couple of small modifications. The sort I didn’t use because my database query has it sorted by date then by name. And then for the end result I was able to format it specially to show the user’s facebook picture using the graph image. I can’t thank you enough for helping me get this working! Thanks a ton!

Here’s some quickly run-up code to achieve that.


<?php
$events = array( 
    array( 
        'uid' => 111,
        'birthday_date' => '1958-06-02',
        'name' => 'Name 1'
    ), 
    array( 
        'uid' => 222,
        'birthday_date' => '1958-06-03',
        'name' => 'Name 2'
    ),
    array( 
        'uid' => 333,
        'birthday_date' => '1958-06-03',
        'name' => 'Name 3'
    ),
    array( 
        'uid' => 444,
        'birthday_date' => '1958-06-05',
        'name' => 'Name 4'
    ) 
);
function calendar($events) {
    // divide in to days
    $days = array();
    foreach ($events as $event) {
        $day = date('d', strtotime($event['birthday_date']));
        $days[intval($day)][] = $event;
    }
    // summarise each day
    $calendar = array();
    foreach ($days as $day => $events) {
        // sort by name
        usort($events, function ($a, $b) {
            return $b['name'] < $a['name'];
        });
        // collate
        $entries = 0;
        $desc = array();
        $prevname = '';
        foreach ($events as $event) {
            $entries += 1;
            if ($event['name'] <> $prevname) {
                $desc[] = $event['name'] . ', ' . $event['uid'];
            }
        }
        $calendar[$day] = $entries . ' entr' . ($entries === 1 ? 'y' : 'ies') . ', ' .
            implode(' - ', $desc);
    }
    return $calendar;
}
$calendar = calendar($events);
echo '<pre>';
var_dump($calendar);
echo '</pre>';
?>

which results in:


array(3) {
  [2]=>
  string(19) "1 entry Name 1, 111"
  [3]=>
  string(35) "2 entries Name 2, 222 - Name 3, 333"
  [5]=>
  string(19) "1 entry Name 4, 444"
}