skip to Main Content

I have a date range 2023-11-01 to 2024-01-04 and some date have different MinStay value.

This is my array of date range

$input = [
  [ 'date' => '2023-11-01', 'MinStay' => 1 ],
  [ 'date' => '2023-11-02', 'MinStay' => 1 ],
  [ 'date' => '2023-11-03', 'MinStay' => 1 ],
  [ 'date' => '2023-11-04', 'MinStay' => 2 ],
  [ 'date' => '2023-11-05', 'MinStay' => 2 ],
  [ 'date' => '2023-11-06', 'MinStay' => 2 ],
  [ 'date' => '2023-12-10', 'MinStay' => 1 ],
  [ 'date' => '2023-12-11', 'MinStay' => 1 ],
  [ 'date' => '2023-12-12', 'MinStay' => 3 ],
  [ 'date' => '2023-12-13', 'MinStay' => 2 ],
  [ 'date' => '2023-12-14', 'MinStay' => 2 ],
  [ 'date' => '2024-01-01', 'MinStay' => 4 ],
  [ 'date' => '2024-01-02', 'MinStay' => 4 ],
  [ 'date' => '2024-01-03', 'MinStay' => 4 ],
  [ 'date' => '2024-01-04', 'MinStay' => 4 ],
];

And I want to

$output = [
  [ 'dateForm' => '2023-11-01', 'dateTo' => '2023-11-03', 'MinStay' => 1 ],
  [ 'dateForm' => '2023-11-04', 'dateTo' => '2023-11-06', 'MinStay' => 2 ],
  [ 'dateForm' => '2023-12-10', 'dateTo' => '2023-12-11', 'MinStay' => 1 ],
  [ 'dateForm' => '2023-12-12', 'dateTo' => '2023-12-12', 'MinStay' => 3 ],
  [ 'dateForm' => '2023-12-13', 'dateTo' => '2023-12-14', 'MinStay' => 2 ],
  [ 'dateForm' => '2024-01-01', 'dateTo' => '2024-01-04', 'MinStay' => 4 ],
];

How can I solve this issue.

Thank you.

I’m try using this array but it’s not happening.

$arr = [];
foreach($input as $date) {
  if($date['MinStay'] == 1) {
    $arr[] = [
      'dateForm' => $date['date'],
      'dateTo' => $date['date'],
      'MinStay' => $date['MinStay'],
    ];
  }
}

2

Answers


  1. A neat way to do it is to perform two loops:

    $temp = [];
    foreach ($input as $record) {
        if (!isset($temp[$record['MinStay']])) {
            $temp[$record['MinStay']] = [
                'dateFrom' => $record['date'],
                'dateTo' => $record['date']
            ];
        } else if ($temp[$record['MinStay']]['dateFrom'] > $record['date']) {
            $temp[$record['MinStay']]['dateFrom'] = $record['date'];
        } else if ($temp[$record['MinStay']]['dateTo'] < $record['date']) {
            $temp[$record['MinStay']]['dateTo'] = $record['date'];
        }
        $output = [];
        foreach ($temp as $MinStay => $record) {
            $output []= [
                'MinStay' => $MinStay,
                'dateFrom' => $record['dateFrom'],
                'dateTo' => $record['dateTo']
            ];
        }
    }
    

    First, we create a temporary array where we store elements with the key being the value of MinStay. Whenever we process a record, we check whether its MinStay was processed in an earlier record. If not, then we create such a record, with dateFrom and dateTo being the date. Otherwise, we check whether date is lesser than dateFrom, in which case we update dateFrom to it, or, if it is greater than dateTo, then we set dateTo to it.

    Now we have all the groups we want, we just need to convert the format to the output you desired, so we do another loop and convert each element to the format you wanted and insert it into the output array.

    Login or Signup to reply.
  2. Firstly, loop through the array, keep track of the current date range, and compare the MinStay value with the previous one

    If they are the same, extend the range, otherwise, push the current range to the output and start a new one.

    $input = [
          ['date' => '2023-11-01', 'MinStay' => 1],
          ['date' => '2023-11-02', 'MinStay' => 1],
          ['date' => '2023-11-03', 'MinStay' => 1],
          ['date' => '2023-11-04', 'MinStay' => 2],
          ['date' => '2023-11-05', 'MinStay' => 2],
          ['date' => '2023-11-06', 'MinStay' => 2],
          ['date' => '2023-12-10', 'MinStay' => 1],
          ['date' => '2023-12-11', 'MinStay' => 1],
          ['date' => '2023-12-12', 'MinStay' => 3],
          ['date' => '2023-12-13', 'MinStay' => 2],
          ['date' => '2023-12-14', 'MinStay' => 2],
          ['date' => '2024-01-01', 'MinStay' => 4],
          ['date' => '2024-01-02', 'MinStay' => 4],
          ['date' => '2024-01-03', 'MinStay' => 4],
          ['date' => '2024-01-04', 'MinStay' => 4],
    ];
    
    $output = [];
    $currentRange = null; // Will store the current range of dates
    
    foreach ($input as $date) {
        // If currentRange is empty, start a new range
        if ($currentRange === null) {
            $currentRange = [
                'dateForm' => $date['date'],
                'dateTo' => $date['date'],
                'MinStay' => $date['MinStay'],
            ];
        } else {
            // Check if MinStay matches the current range's MinStay
            if ($date['MinStay'] === $currentRange['MinStay']) {
                // Extend the date range
                $currentRange['dateTo'] = $date['date'];
            } else {
                // If MinStay changes, push the current range to the output
                $output[] = $currentRange;
    
                // Start a new range
                $currentRange = [
                    'dateForm' => $date['date'],
                    'dateTo' => $date['date'],
                    'MinStay' => $date['MinStay'],
                ];
            }
        }
    }
    
    // Add the last range to the output
    if ($currentRange !== null) {
        $output[] = $currentRange;
    }
    
    // Output the result
    print_r($output);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search