0

I have this Open Days array of objects for my project.

stdClass Object
(
    [Mon] => stdClass Object
        (
            [DAY_NAME] => Mon
        )

    [Tue] => stdClass Object
        (
            [DAY_NAME] => Tue
        )

    [Wed] => stdClass Object
        (
            [DAY_NAME] => Wed
        )

    [Sat] => stdClass Object
        (
            [DAY_NAME] => Sat
        )

    [Sun] => stdClass Object
        (
            [DAY_NAME] => Sun
            [SPECIAL_START_TIME] => 12:00 am
            [SPECIAL_END_TIME] => 08:54 pm
        )

)

So, when I need to print the from-open-day, I take the first element of the array, and the to-open-day, I take the end element of the array. But in between, Thursday and Friday are closed. The resultant view that I need is

Mon-Wed, Sat - #some-time-here
Sun - #Some-time-here

I am very much confused as to how should I approach this

2
  • Open Days is an object, not an array Commented Mar 30, 2015 at 8:04
  • Yes, mu mistake while typing. But, can I get a solution for this? Commented Mar 30, 2015 at 8:07

1 Answer 1

1

So open-to-close times are the unique entities and days are the related entities for each. This indicates a data structure where open-to-close times are keys in a map of related days. In PHP that would look like this:

$out = array(
    '10:00-18:00' => array('Mon', 'Tue', 'Wed', 'Sat'),
    '12:00-17:00' => array('Sun')
);

Building this array is done with a single loop through that object:

$out = array();
foreach($obj as $day => $data) {
    $time_key = $data->SPECIAL_START_TIME . '-' . $data->SPECIAL_END_TIME;
    if(!array_key_exists($time_key, $out)) { 
        $out[$time_key] = array();
    }
    $out[$time_key][] = $day;
}

Now you have to "compact" consecutive days in day ranges (array('Mon', 'Tue', 'Wed') becomes 'Mon-Wed'):

$order = array(
    'Sun' => 0,
    'Mon' => 1,
    'Tue' => 2,
    'Wed' => 3,
    'Thu' => 4,
    'Fri' => 5,
    'Sat' => 6
);

$in = array('Mon', 'Tue', 'Thu', 'Fri', 'Sat', 'Sun');

function group_days($in, $order) {
    $out = array();
    $outIndex = -1;

    $prevIndex = -1;
    foreach($in as $day) {
        if($order[$day] - $prevIndex !== 1 || $outIndex === -1) {
            $outIndex += 1;
            $out[$outIndex] = array();
        }
        $prevIndex = $order[$day];
        $out[$outIndex][] = $day;
    }

    return $out;
}

$groups = group_days($in, $order);
foreach($groups as $group) {
    if(count($group) === 1) {
        echo $group[0] . PHP_EOL;
    }
    else {
        echo $group[0] . '-' . $group[count($group) -1] . PHP_EOL;
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

How do I compact that? That was my actual question

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.