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 8734187
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:47:29+00:00 2026-06-13T09:47:29+00:00

This is a very odd problem and I’m sorry in advance about the title

  • 0

This is a very odd problem and I’m sorry in advance about the title of the question, but I can’t think of a good one.

Anyway, I have an array of prices, one for every day of the week.

 array = (
   [Mon] => 0.40,
   [Tue] => 0.40,
   [Wed] => 0.40,
   [Thu] => 0.40,
   [Fri] => 0.40,
   [Sat] => 0.50,
   [Sun] => 
 )

As you can see one or more days can be empty and the prices can be different every day.

I need to get a string like

 Mon - Fri 0.40, Sat 0.50

I kind of need to flat the array, as long as the price is the same.

The one above is just an example, I may have “holes” in the array, so for example if I didn’t have a price for Wednesday, then the resulting string should be

 Mon - Tue 0.40, Thu - Fri 0.40, Sat 0.50

Any suggestion how I can do this in PHP?

  • 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-06-13T09:47:31+00:00Added an answer on June 13, 2026 at 9:47 am

    You can try this …

    Your array

    $array = array(
            "Mon" => 0.40,
            "Tue" => 0.40,
            "Wed" => 0.40,
            "Thu" => 0.40,
            "Fri" => 0.40,
            "Sat" => 0.50,
            "Sun" => null);
    

    Example 1

    var_dump(groupDateTime($array));
    

    Output

    array
      0 => 
        array
          's' => string 'Mon' (length=3)
          'e' => string 'Fri' (length=3)
          't' => float 0.4
      1 => 
        array
          's' => string 'Sat' (length=3)
          'e' => string 'Sat' (length=3)
          't' => float 0.5
    

    Example 2

     var_dump(groupDateTime($array,true));
                                    ^---------- Optional Formatting 
    

    Output

    array
      0 => string 'Mon - Fri 0.4' (length=13)
      1 => string 'Sat 0.5' (length=7)
    

    Function Used

    function groupDateTime($array, $format = false) {
        $group = array();
        $x = 0;
        foreach ( $array as $day => $time ) {
            if (empty($group[$x]) === true) {
                $group[$x]['s'] = $day;
                $group[$x]['e'] = $day;
                $group[$x]['t'] = $time;
            }
            if ($group[$x]['t'] === $time) {
                $group[$x]['e'] = $day;
                continue;
            }
            $x ++;
            $group[$x]['s'] = $day;
            $group[$x]['e'] = $day;
            $group[$x]['t'] = $time;
        }
    
        $group = array_filter($group, function ($v) {
            return ! empty($v['t']);
        });
        if ($format === true)
            $group = array_map(function ($v) {
                if ($v['s'] == $v['e'])
                    return "{$v['s']} {$v['t']}";
                return "{$v['s']} - {$v['e']} {$v['t']}";
            }, $group);
        return $group;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is my first question on S.O. I have a very odd problem. Below
I am facing a very odd problem. Can any one tell me what is
This is a very odd one and I'm not sure how to proceed. Basically,
Very odd problem. This function has randomly started hanging and timing out when I
I've been experiencing this very odd problem lately in Heroku. I develop and test
Currently having this really odd problem with my layout IE7. I'm not very experienced
This seems like an odd problem. Maybe it's also the wrong approach. But I
I have this very odd problem with Java (using Eclipse as IDE). I'm working
This is a very odd problem. I include the .h file of the next
This seems to be a very odd problem that I cannot figure out for

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.