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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T19:05:14+00:00 2026-06-09T19:05:14+00:00

I am trying to get the array elements within a range but failed to

  • 0

I am trying to get the array elements within a range but failed to do so. Explaining it below.

$date_array = array('2012-08-02','2012-08-09','2012-08-16','2012-08-23');
$start_date = '2012-08-01';
$end_date   = '2012-08-10';

I want to get the array elements from the $date_array within $start_date and $end_date.
i.e, output will be : 2012-08-02 and 2012-08-09.

Edit:

The array can be the following as well.

$date_array = array('2012-08-02','2012-08-10','2012-08-16','2012-08-23');
  • 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-09T19:05:16+00:00Added an answer on June 9, 2026 at 7:05 pm

    You can do that using array_filterDocs and a callback that fulfills your needs:

    $filter = function($start, $end) {
        return function($string) use ($start, $end) {
            return $string >= $start && $string <= $end;
        };
    };
    
    $result = array_filter($array, $filter('2012-08-01', '2012-08-10'));
    

    Take care of the order of the parameters and as well that you have these exact formats because only those can the done with a simple string comparison.


    For PHP 5.2 compatibility and also to solve this for iterators and not only arrays, here is a more general approach:

    class Range
    {
        private $from;
        private $to;
        public function __construct($from, $to) {
            $this->from = $from;
            $this->to = $to;
            if ($from > $to) {
                $this->reverse();
            }
        }
        private function reverse() {
            list($this->from, $this->to) = array($this->to, $this->from);
        }
        public function in($value) {
            return $this->from <= $value && $value <= $this->to;
        }
    }
    
    class RangeFilter extends FilterIterator
    {
        private $range;
        public function __construct(Iterator $iterator, Range $range) {
            $this->range = $range;
            parent::__construct($iterator);
        }
    
        public function accept()
        {
            $value = $this->getInnerIterator()->current();
            return $this->range->in($value);
        }
    }
    
    $range = new Range($start, $end);
    $it = new ArrayIterator($array);
    $filtered = new RangeFilter($it, $range);
    $result = iterator_to_array($filtered);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to get an array of all elements with the class sampleclass. For
I'm trying to get the values from a pointer to a float array, but
I am trying to get all the form elements within a specific div, and
I am trying to access elements within an associative array. However, I do not
I'm trying to get all form elements to array, then loop through them using
I'm trying to get the index of an element within an array returned by
I am trying to get an array of a date plus the next 13
Trying to get a value out of 2d array inside of a hash and
I'm trying to get one object from this array and print all of its
I'm trying to get a table form a numpy array at the barplot bottom

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.