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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T11:43:12+00:00 2026-05-31T11:43:12+00:00

I’m looking to process this array of dates and an essentially boolean single value:

  • 0

I’m looking to process this array of dates and an essentially boolean single value:

Array (
    [20120401] => 1
    [20120402] => 1
    [20120403] => 1
    [20120404] => 0
    [20120405] => 0
    [20120406] => 0
    [20120407] => 1
    [20120408] => 1
    [20120409] => 1
)

Into this:

Array (
    Array(
        'start_date' => '20120401',
        'end_date' => '20120403',
        'status' => '1'
        ),
    Array(
        'start_date' => '20120404',
        'end_date' => '20120406',
        'status' => '0'
        ),
    Array('start_date' => '20120407',
          'end_date' => '20120409',
          'status' => '1'
        ),
)

So essentially splitting the array when the value changes and creating a new array with the start key and end key of where the value changed and the value of that section. Sorry if the explanation doesn’t make as much sense as the example arrays!

It seems on the face of it to me perhaps a recursive function would suit whereby the 1st example arrays values get removed when processed and the function is called again with the shortened array. I could be missing something simple with this though!

Thanks!

EDIT:

Just to update this, I should have made it clearer in the question that the original array will span month divisions so I updated the code provided by hakre to take account of this. I also modified it to use the desired output array keys. In my case I do have control of the input array so can validate the dates before being passed. Thanks again hakre.

//set the time period in seconds to compare, daily in this case
$time_period = (60 * 60 * 24);

$output = array();
$current = array();
foreach($input as $date => $state) {
    //convert date to UTC
    $date = strtotime($date);

    if (!$current || $current['state'] != $state ||
        $current['to'] != $date - $time_period) {
        unset($current);
        $current = array('state' => $state, 'from' => $date, 'to' => $date);
        $output[] = &$current;
        continue;
    }
    $current['to'] = $date;
}
unset($current);

// convert date back to the desired format
foreach ( $output as $index => $section ) {
    $output[$index]['from'] = date("Ymd", $output[$index]['from'] );
    $output[$index]['to'] = date("Ymd", $output[$index]['to'] );
}
  • 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-31T11:43:13+00:00Added an answer on May 31, 2026 at 11:43 am

    There is no need for recursion because you can solve this iteratively. You build the current element unless the conditions are matched to create a new element. The trick is to make the first current element invalid per default, so the first entry will create a new one:

    $output = array();
    $current = array();
    foreach($input as $date => $status) {
        if (!$current || $current[2] != $status || $current[1] != $date-1) {
            unset($current);
            $current = array($date, $date, $status);
            $output[] = &$current;
            continue;
        }
        $current[1] = $date;
    }
    unset($current);
    

    Which will give you the following result for $output:

    Array
    (
        [0] => Array
            (
                [0] => 20120401
                [1] => 20120403
                [2] => 1
            )
    
        [1] => Array
            (
                [0] => 20120404
                [1] => 20120406
                [2] => 0
            )
    
        [2] => Array
            (
                [0] => 20120407
                [1] => 20120409
                [2] => 1
            )
    
    )
    

    Apart from the named keys which are numbered here, this is what you’re looking for.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka

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.