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

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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:35:54+00:00 2026-05-26T06:35:54+00:00

I am given data from an API in a format that is pretty difficult

  • 0

I am given data from an API in a format that is pretty difficult to use (in the mannor that is necessary to be used in) – I do not have the ability to modify the API. Below is an example of the data I am given:

    $data = array(
        array('day' => 'Monday', 'start' => 1100, 'end' => 1300),
        array('day' => 'Tuesday', 'start' => 1100, 'end' => 1300),
        array('day' => 'Wednesday', 'start' => 1100, 'end' => 1300),
        array('day' => 'Thursday', 'start' => 1200, 'end' => 1300),
        array('day' => 'Friday', 'start' => 1200, 'end' => 1300),
        array('day' => 'Saturday', 'start' => 1200, 'end' => 1300),
        array('day' => 'Sunday', 'start' => 1200, 'end' => 1400)
    );

The data may not include all seven days of the week, start and end vary between 0000 and 2400. I am trying to come up with a way to format the data with the following output: Monday - Wednesday (1100-1300), Thursday - Saturday (1200-1300), Sunday (1200-1400) basically, contiguous days (whose start and end times are the same) are separated by a hypen.

I am kind of trying to avoid a huge block of ugly code

  • 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-05-26T06:35:55+00:00Added an answer on May 26, 2026 at 6:35 am

    Ugliness is in the eye of the beholder. 😉 I think this is OK, but of course it might be exactly what you’re trying to avoid.

    <?php
    $data = array(
        array('day' => 'Monday', 'start' => 1100, 'end' => 1300),
        array('day' => 'Tuesday', 'start' => 1100, 'end' => 1300),
        array('day' => 'Wednesday', 'start' => 1100, 'end' => 1300),
        array('day' => 'Thursday', 'start' => 1200, 'end' => 1300),
        array('day' => 'Friday', 'start' => 1200, 'end' => 1300),
        array('day' => 'Saturday', 'start' => 1200, 'end' => 1300),
        array('day' => 'Sunday', 'start' => 1200, 'end' => 1400)
    );
    
    $periods = array();
    
    $start = $end = $i = -1;
    
    foreach ($data as $datum) {
        if ($start != $datum['start'] || $end != $datum['end']) {
            $i++;
            $start = $datum['start'];
            $end   = $datum['end'];
    
            $periods[$i] = array(
                              'start'    => $start, 
                              'end'      => $end,
                              'startDay' => $datum['day']);
        }
        $periods[$i]['endDay'] = $datum['day'];
    }
    
    foreach ($periods as $k => $period) {
        echo ($k) ? ', ' : '';
        if ($period['startDay'] === $period['endDay']) {
            echo $period['startDay'];
        } else {
            echo "{$period['startDay']} - {$period['endDay']}";
        }
        echo " ({$period['start']} - {$period['end']})";
    }
    

    Would output:

    Monday - Wednesday (1100 - 1300), Thursday - Saturday (1200 - 1300), Sunday (1200 - 1400)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a little app that pulls data from several API's. In all of
I get data from a 3rd party API that just gives me back a
I have an application that once started will get some initial data from my
I have a Silverlight Windows Phone 7 app that pulls data from a public
I'm using jQuery's $.getJSON() API to retrieve data from a given URL for a
I'm creating an iPhone app that will pull data down from a Web API,
I'm reading data from a stream into a char array of a given length,
We've been given the task by my customer to export data from a legacy
I'm being given a data source weekly that I'm going to parse and put
I know you could make a helper pretty easily given the data. So, if

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.