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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T08:57:32+00:00 2026-05-30T08:57:32+00:00

I’m having an asbolute nightmare dealing with an array of numbers which has the

  • 0

I’m having an asbolute nightmare dealing with an array of numbers which has the following structure :

Odd numbers in the array : NumberRepresenting Week
Even numbers in the array : NumberRepresenting Time

So for example in the array :

index : value
0 : 9
1 : 1
2 : 10
3 : 1

Would mean 9 + 10 on Day 1 (Monday).

The problem is, I have a an unpredictable number of these and I need to work out how many “sessions” there are per day. The rules of a session are that if they are on a different day they are automatically different sessions. If they are next to each other like in the example 9 + 10 that would count as a single session. The maximum number than can be directly next to eachother is 3. After this there needs to be a minimum of a 1 session break in between to count as a new session.

Unfortunately, we cannot also assume that the data will be sorted. It will always follow the even / odd pattern BUT could potentially not have sessions stored next to each other logically in the array.

I need to work out how many sessions there are.

My code so far is the following :

for($i = 0; $i < (count($TimesReq)-1); $i++){
    $Done = false;
    if($odd = $i % 2 )
    {
    //ODD WeekComp
        if(($TimesReq[$i] != $TimesReq[$i + 2])&&($TimesReq[$i + 2] != $TimesReq[$i + 4])){
        $WeeksNotSame = true;
        }
    }
    else
        {
        //Even TimeComp

        if(($TimesReq[$i] != ($TimesReq[$i + 2] - 1))&& ($TimesReq[$i + 2] != ($TimesReq[$i + 4] - 1)))
        $TimesNotSame = true; 
        }
    if($TimesNotSame == true && $Done == false){
    $HowMany++; 
    $Done = true;
    }
    if($WeeksNotSame == true && $Done == false){
    $HowMany++;
    $Done = true; 
    }
$TimesNotSame = false;
$WeeksNotSame = false;
    }

However this isn’t working perfectly. for example it does not work if you have a single session and then a break and then a double session. It is counting this as one session.

This is, probably as you guessed, a coursework problem, but this is not a question out of a textbook, it is part of a timetabling system I am implementing and is required to get it working. So please don’t think i’m just copy and pasting my homework to you guys!

Thank you so much!

New Code being used :

if (count($TimesReq) % 2 !== 0) {
//throw new InvalidArgumentException();
}

for ($i = 0; $i < count($TimesReq); $i += 2) {

    $time = $TimesReq[$i];
    $week = $TimesReq[$i + 1];

    if (!isset($TimesReq[$i - 2])) {
        // First element has to be a new session
        $sessions += 1;
                $StartTime[] = $TimesReq[$i];
                $Days[] = $TimesReq[$i + 1];
        continue;
    }

    $lastTime = $TimesReq[$i - 2];
    $lastWeek = $TimesReq[$i - 1];

    $sameWeek = ($week === $lastWeek);
    $adjacentTime = ($time - $lastTime === 1);
    if (!$sameWeek || ($sameWeek && !$adjacentTime)) {

    if(!$sameWeek){//Time
    $Days[] = $TimesReq[$i + 1];
            $StartTime[] = $TimesReq[$i];
            $looking = true;
    }
    if($sameWeek && !$adjacentTime){
    }
    if($looking && !$adjacentTime){
        $EndTime[] = $TimesReq[$i];
        $looking = false;           
    }
//Week      

        $sessions += 1;
    }
}
  • 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-30T08:57:33+00:00Added an answer on May 30, 2026 at 8:57 am

    If you want a single total number of sessions represented in the data, where each session is separated by a space (either a non-contiguous time, or a separate day). I think this function will get you your result:

    function countSessions($data)
    {
        if (count($data) % 2 !== 0) throw new InvalidArgumentException();
    
        $sessions = 0;
        for ($i = 0; $i < count($data); $i += 2) {
            $time = $data[$i];
            $week = $data[$i + 1];
    
            if (!isset($data[$i - 2])) {
                // First element has to be a new session
                $sessions += 1;
                continue;
            }
    
            $lastTime = $data[$i - 2];
            $lastWeek = $data[$i - 1];
    
            $sameWeek = ($week === $lastWeek);
            $adjacentTime = ($time - $lastTime === 1);
            if (!$sameWeek || ($sameWeek && !$adjacentTime)) {
                $sessions += 1;
            }
        }
    
        return $sessions;
    }
    
    $totalSessions = countSessions(array(
        9, 1,
        10, 1,
    ));
    

    This of course assumes the data is sorted. If it is not, you will need to sort it first. Here is an alternate implementation that includes support for unsorted data.

    function countSessions($data)
    {
        if (count($data) % 2 !== 0) throw new InvalidArgumentException();
    
        $slots = array();
        foreach ($data as $i => $value) {
            if ($i % 2 === 0) $slots[$i / 2]['time'] = $value;
            else $slots[$i / 2]['week'] = $value;
        }
    
        usort($slots, function($a, $b) {
            if ($a['week'] == $b['week']) {
                if ($a['time'] == $b['time']) return 0;
                return ($a['time'] < $b['time']) ? -1 : 1;
            } else {
                return ($a['week'] < $b['week']) ? -1 : 1;
            }
        });
    
        $sessions = 0;
        for ($i = 0; $i < count($slots); $i++) {
            if (!isset($slots[$i - 1])) { // First element has to be a new session
                $sessions += 1;
                continue;
            }
    
            $sameWeek = ($slots[$i - 1]['week'] === $slots[$i]['week']);
            $adjacentTime = ($slots[$i]['time'] - $slots[$i - 1]['time'] === 1);
            if (!$sameWeek || ($sameWeek && !$adjacentTime)) {
                $sessions += 1;
            }
        }
    
        return $sessions;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

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.