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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:54:17+00:00 2026-05-27T18:54:17+00:00

I have an array of items that all have start and end dates associated

  • 0

I have an array of items that all have start and end dates associated with them. I’d like to group them according to closest start and end dates without any of them overlapping, but I’m having difficulty conjuring what the logic for this should look like.

Let’s say I start with this:

$query = array(
[0] =>
   array(
   [0] =>
        array(
        ['name'] =>'A'
        ['start'] =>'1/1/2011'
        ['end'] =>'1/31/2011'
        )
   [1] =>
        array(
        ['name'] =>'B'
        ['start'] =>'1/15/2011'
        ['end'] =>'1/31/2011'
        )
   [2] =>
        array(
        ['name'] =>'C'
        ['start'] =>'2/1/2011'
        ['end'] =>'2/28/2011'
        )
   [3] =>
        array(
        ['name'] =>'D'
        ['start'] =>'2/2/2011'
        ['end'] =>'2/28/2011'
        )
   [4] =>
        array(
        ['name'] =>'E'
        ['start'] =>'1/31/2011'
        ['end'] =>'3/1/2011'
        )
   [5] =>
        array(
        ['name'] =>'F'
        ['start'] =>'3/3/2011'
        ['end'] =>'3/31/2011'
        )
    )
)

And I want to finish with this:

$result = array(
[0] =>
   array(
   [0] =>
        array(
        ['name'] =>'A'
        ['start'] =>'1/1/2011'
        ['end'] =>'1/31/2011'
        )

   [1] =>
        array(
        ['name'] =>'C'
        ['start'] =>'2/1/2011'
        ['end'] =>'2/28/2011'
        )
   [2] =>
        array(
        ['name'] =>'F'
        ['start'] =>'3/3/2011'
        ['end'] =>'3/31/2011'
        )
   )

[1]=> 
   array(
   [0] =>
        array(
        ['name'] =>'B'
        ['start'] =>'1/15/2011'
        ['end'] =>'1/31/2011'
        )

   [1] =>
        array(
        ['name'] =>'D'
        ['start'] =>'2/2/2011'
        ['end'] =>'2/28/2011'
        )
   )

[2]=>
   array(
   [0] =>
        array(
        ['name'] =>'E'
        ['start'] =>'1/31/2011'
        ['end'] =>'3/1/2011'
        )

    )
)

edit: by request, the var_export for the input and output listed above:

$query = array ( 0 => array ( 'name' => 'A', 'start' => '1/1/2011', 'end' => '1/31/2011', ), 1 => array ( 'name' => 'B', 'start' => '1/15/2011', 'end' => '1/31/2011', ), 2 => array ( 'name' => 'C', 'start' => '2/1/2011', 'end' => '2/28/2011', ), 3 => array ( 'name' => 'D', 'start' => '2/2/2011', 'end' => '2/28/2011', ), 4 => array ( 'name' => 'E', 'start' => '1/31/2011', 'end' => '3/1/2011', ), 5 => array ( 'name' => 'F', 'start' => '3/3/2011', 'end' => '3/31/2011', ), ) 

$result = array ( 0 => array ( 0 => array ( 'name' => 'A', 'start' => '1/1/2011', 'end' => '1/31/2011', ), 1 => array ( 'name' => 'C', 'start' => '2/1/2011', 'end' => '2/28/2011', ), 2 => array ( 'name' => 'F', 'start' => '3/3/2011', 'end' => '3/31/2011', ), ), 1 => array ( 0 => array ( 'name' => 'B', 'start' => '1/15/2011', 'end' => '1/31/2011', ), 1 => array ( 'name' => 'D', 'start' => '2/2/2011', 'end' => '2/28/2011', ), ), 2 => array ( 0 => array ( 'name' => 'E', 'start' => '1/31/2011', 'end' => '3/1/2011', ), ), )

The best I have so far is to loop through the items in $query, and for each one, compare the start date to the end date of the last item in each of the arrays. Even as I’m typing this out I’m realizing that that would assume we’re starting with a $query array that’s already in some sort of chronological order (it’s random.)

We are trying to put a GANTT timeline together with this data, using as few rows as possible to help visualize what our open schedule looks like. It has me stumped. Anyone who can point to the most efficient method of organizing these objects, it’d be greatly appreciated.

  • 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-27T18:54:18+00:00Added an answer on May 27, 2026 at 6:54 pm

    I have what I believe to be the start of the answer here.

    I’m looping through the $query and using unset() on each object as it’s added to arrays within $result.

    Hopefully what’s written here is as efficient as possible:

    function cmp($a, $b){ 
    return strcmp($a['start'], $b['start']); 
    }
    
    $query = array(array('name' =>'A','start' =>'1/1/2011','end' =>'1/31/2011'),array('name' =>'B','start' =>'1/15/2011','end' =>'1/31/2011'),array('name' =>'C','start' =>'2/1/2011','end' =>'2/28/2011'),array('name' =>'D','start' =>'2/2/2011','end' =>'2/28/2011'),array('name' =>'E','start' =>'1/31/2011','end' =>'3/1/2011'), array('name' =>'F','start' =>'3/3/2011','end' =>'3/31/2011'));
    
    usort($query, "cmp"); // organize by start date
    
    $result = array(); 
    
    $c = count($query);
    for($i = 1; $i <= $c; $i++) {
    if (empty($result)) {
        $result[0][0] = $query[0];
        unset($query[0]);
        $query = array_values($query);  
    
    } else {
    
        $lastkey_group = array_pop(array_keys($result)); 
        $lastkey_object = array_pop(array_keys($result[$lastkey_group]));
    
        // get the last object's end date
        $last_end_date = strtotime($result[$lastkey_group][$lastkey_object]['end']);
    
        $match_days = 1000;
        $match_key = 1000;
    
        foreach($query as $key => $q) {
            $this_start_date = strtotime($q['start']);
            $diff = round(($this_start_date - $last_end_date)/86400);
            // compare to start date in each $q
            if($diff < $match_days && $diff >= 0) {                     
                // if the distance is greater than 0 but less than $diff, 
                // replace match with distance and row key
                $match_days = $diff;
                $match_key = $key;
            } 
        }
    
    
        if($match_key == 1000) {                
            $result[$lastkey_group + 1][0] = $query[0]; // no good matches. write to a new group 
            unset($query[0]);       
            $query = array_values($query);
    
        } else {
            $result[$lastkey_group][$lastkey_object + 1] = $query[$match_key]; 
                // match. write to this group
            unset($query[$match_key]);
            $query = array_values($query);
        }   
    }
    }
     var_dump($result);
    

    And here’s the output of this:

    array(3) { [0]=> array(3) { [0]=> array(3) { ["name"]=> string(1) "A" ["start"]=> string(8) "1/1/2011" ["end"]=> string(9) "1/31/2011" } [1]=> array(3) { ["name"]=> string(1) "E" ["start"]=> string(9) "1/31/2011" ["end"]=> string(8) "3/1/2011" } [2]=> array(3) { ["name"]=> string(1) "F" ["start"]=> string(8) "3/3/2011" ["end"]=> string(9) "3/31/2011" } } [1]=> array(2) { [0]=> array(3) { ["name"]=> string(1) "B" ["start"]=> string(9) "1/15/2011" ["end"]=> string(9) "1/31/2011" } [1]=> array(3) { ["name"]=> string(1) "C" ["start"]=> string(8) "2/1/2011" ["end"]=> string(9) "2/28/2011" } } [2]=> array(1) { [0]=> array(3) { ["name"]=> string(1) "D" ["start"]=> string(8) "2/2/2011" ["end"]=> string(9) "2/28/2011" } } } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an array of items that are time sensitive. After an amount of
I have an array of items that should be selected in my tree control.
I have an array that has 22 items in it (it may also have
I'm looking for a kind of array data-type that can easily have items added,
I have an array of 50 items and I would like to choose 5
I have an array with lots of items with same names like CloudObserverCMSStub edited
I have a simple array with x number of items. I am displaying them
I have this little code which lists all matches of items that contains the
I have an array Items which has 10 elements. I need to remove the
I have a 10x10 array in Java, some of the items in array which

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.