osakagreg
osakagreg

Reputation: 577

Create and sort array to arrange events by time

The data I have to work with looks like this:

<div><strong><?php echo $form_data['field'][86]);?></strong> - Guests find seats</div>
<div><strong><?php echo $form_data['field'][87];?></strong> - Bridal party introductions</div>
<div><strong><?php echo $form_data['field'][88];?></strong> - Dinner is served</div>
<div><strong><?php echo $form_data['field'][92];?></strong> - Cake cutting</div>
<div><strong><?php echo $form_data['field'][96];?></strong> - Speeches</div>
<div><strong><?php echo $form_data['field'][188];?></strong> - Blessing</div>
<div><strong><?php echo $form_data['field'][107];?></strong> - First Dances</div>

But that example data is rarely consistent. For example, there may not be a cake cutting, also the order as shown is not correct.

However, the $form_data values are ALWAYS 24 hour fomats (ie. 14:00, 03:30, etc.)

I want to be able to take all the existing strings (whichever one's are not blank) and create an array that includes the time and event type and then echoes the output in the correct order.)

So the array would always include those 7 string values, but there may be times that only 5 or 6 of them are relevant.

Thank you for any help you can provide.

Upvotes: 1

Views: 71

Answers (1)

RiggsFolly
RiggsFolly

Reputation: 94662

How about this for an idea. I think I follow your question, but dont beat me up if I missed something.

Write a little PHP to create a seperate array from the relevant items from $form_data array, add the labels to that array as well so you know what you are putting out onto the page

<?php
    $pickupList = array(86  => 'Guests find seats',
                        87  => 'Bridal party introductions',
                        88  => 'Dinner is served',
                        92  => 'Cake cutting',
                        96  => 'Speeches',
                        188 => 'Blessing',
                        107 => 'First Dances'
                       );

    $event_order = array();

    foreach ( $pickupList as $key=> $label ) {

        if ( $form_data['field'][$key] != '' ) {
            $event_order[$form_data['field'][$key]]  = $label;
        }
    }  

    ksort($event_order);    // sort on key which is a time 12:15

    foreach ( $event_order as $time => $event) {
        echo '<div><strong>';
        echo $time;
        echo '</strong> - ';
        echo $event;
        echo '</div>';
    }

Upvotes: 2

Related Questions