3v4l.org

run code in 300+ PHP versions simultaneously
<?php function mergeRanges($array){ usort($array,function($a,$b){ return $a['start_date']<=>$b['start_date']; }); // order by start_date ASC foreach($array as $i=>$row){ if($i && $row['start_date']<=date('Y-m-d',strtotime("{$result[$x]['end_date']} +1 day"))){ // not the first iteration and dates are within current group's range if($row['end_date']>$result[$x]['end_date']){ // only if current end_date is greater than existing end_date $result[$x]['end_date']=$row['end_date']; // overwrite end_date with new end_date in group } $result[$x]['merged_ids'][]=$row['id']; // append id to merged_ids subarray }else{ // first iteration or out of range; start new group if($i){ // if not first iteration $result[$x]['merged_ids']=implode(', ',$result[$x]['merged_ids']); // convert previous group's id elements to csv string }else{ // first iteration $x=-1; // declare $x as -1 so that it becomes 0 when incremented with ++$x } $result[++$x]=['merged_ids'=>[$row['id']],'start_date'=>$row['start_date'],'end_date'=>$row['end_date']]; // declare new group } } $result[$x]['merged_ids']=implode(', ',$result[$x]['merged_ids']); // convert final merged_ids subarray to csv string return $result; } $array=[ ['id'=>18298,'start_date'=>'2011-07-09','end_date'=>'2011-10-01'], ['id'=>18297,'start_date'=>'2011-06-01','end_date'=>'2011-06-30'], ['id'=>17113,'start_date'=>'2011-03-31','end_date'=>'2011-05-31'], // tests that 17113 and 18297 belong in same group ['id'=>20556,'start_date'=>'2011-02-03','end_date'=>'2011-02-13'], // tests that "fully overlapped" date range is included ['id'=>20555,'start_date'=>'2011-01-03','end_date'=>'2011-03-31'] ]; var_export(mergeRanges($array));

Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).

VersionSystem time (s)User time (s)Memory (MiB)
8.3.60.0090.00618.55
8.3.50.0110.00718.20
8.3.40.0090.00919.09
8.3.30.0110.00418.99
8.3.20.0040.00420.38
8.3.10.0040.00423.53
8.3.00.0080.00019.80
8.2.180.0090.00925.92
8.2.170.0040.01119.16
8.2.160.0120.00622.96
8.2.150.0080.00024.18
8.2.140.0080.00024.66
8.2.130.0070.00026.16
8.2.120.0000.00721.03
8.2.110.0070.00320.33
8.2.100.0070.00718.03
8.2.90.0030.00618.16
8.2.80.0040.00417.97
8.2.70.0090.00018.18
8.2.60.0040.00418.34
8.2.50.0070.00418.10
8.2.40.0000.00720.63
8.2.30.0040.00419.52
8.2.20.0040.00418.20
8.2.10.0030.00618.15
8.2.00.0000.00818.44
8.1.280.0060.01625.92
8.1.270.0090.00020.66
8.1.260.0080.00026.35
8.1.250.0030.00628.09
8.1.240.0090.00022.67
8.1.230.0070.00422.82
8.1.220.0030.00517.79
8.1.210.0050.00318.91
8.1.200.0100.00017.59
8.1.190.0040.00417.60
8.1.180.0060.00319.13
8.1.170.0080.00018.86
8.1.160.0030.00619.07
8.1.150.0040.00420.27
8.1.140.0030.00617.90
8.1.130.0000.00919.04
8.1.120.0080.00017.71
8.1.110.0070.00017.74
8.1.100.0040.00417.66
8.1.90.0040.00417.65
8.1.80.0020.00517.72
8.1.70.0000.00717.75
8.1.60.0080.00017.79
8.1.50.0030.00517.70
8.1.40.0000.00817.70
8.1.30.0030.00617.86
8.1.20.0040.00417.83
8.1.10.0090.00017.79
8.1.00.0030.00517.78
8.0.300.0050.00320.02
8.0.290.0000.00717.00
8.0.280.0030.00318.51
8.0.270.0040.00316.98
8.0.260.0000.00717.00
8.0.250.0080.00017.14
8.0.240.0030.00317.09
8.0.230.0090.00017.14
8.0.220.0000.00717.08
8.0.210.0000.00717.13
8.0.200.0070.00017.25
8.0.190.0050.00217.20
8.0.180.0030.00617.20
8.0.170.0000.00817.09
8.0.160.0000.00817.18
8.0.150.0000.00717.15
8.0.140.0070.00017.02
8.0.130.0030.00313.45
8.0.120.0000.00817.14
8.0.110.0040.00417.08
8.0.100.0030.00317.15
8.0.90.0030.00517.20
8.0.80.0100.00717.16
8.0.70.0020.00517.17
8.0.60.0000.00817.16
8.0.50.0000.00717.19
8.0.30.0080.00917.34
8.0.20.0080.01117.41
8.0.10.0000.00717.32
8.0.00.0080.01016.94
7.4.330.0050.00015.55
7.4.320.0060.00016.73
7.4.300.0000.00716.61
7.4.290.0030.00316.57
7.4.280.0040.00416.71
7.4.270.0000.00716.73
7.4.260.0000.00816.69
7.4.250.0080.00016.63
7.4.240.0070.00016.83
7.4.230.0000.00716.51
7.4.220.0050.00316.56
7.4.210.0020.01216.74
7.4.200.0000.00716.91
7.4.160.0080.01016.76
7.4.150.0000.01817.40
7.4.140.0100.00817.86
7.4.130.0180.00316.74
7.4.120.0130.00716.73
7.4.110.0070.01116.70
7.4.100.0140.01116.71
7.4.90.0040.01416.70
7.4.80.0090.00919.39
7.4.70.0090.00916.94
7.4.60.0060.00916.86
7.4.50.0060.01016.74
7.4.40.0130.00316.83
7.4.20.0120.00416.63
7.4.10.0100.00314.98
7.4.00.0110.00815.33
7.3.330.0000.00713.60
7.3.320.0060.00013.50
7.3.310.0030.00316.64
7.3.300.0060.00316.59
7.3.290.0000.00816.47
7.3.280.0080.00916.58
7.3.270.0070.01117.40
7.3.260.0100.01216.73
7.3.240.0120.00616.69
7.3.230.0080.01116.76
7.3.210.0130.01016.74
7.3.200.0100.00616.69
7.3.190.0070.01016.66
7.3.180.0100.00716.65
7.3.170.0120.00616.70
7.3.160.0100.01316.64
7.3.130.0030.01414.88
7.3.120.0090.00915.18
7.3.110.0080.01014.95
7.3.100.0100.00814.93
7.3.90.0070.00815.06
7.3.80.0070.01015.02
7.3.70.0090.00914.91
7.3.60.0070.00915.10
7.3.50.0080.00815.02
7.3.40.0540.00414.98
7.3.30.0110.00514.91
7.3.20.0350.01016.03
7.3.10.0210.00816.01
7.3.00.0280.00616.11
7.2.330.0110.00717.04
7.2.320.0210.00217.11
7.2.310.0090.00916.93
7.2.300.0130.01016.83
7.2.290.0110.01116.77
7.2.260.0030.01315.55
7.2.250.0110.00715.20
7.2.240.0070.00915.37
7.2.230.0070.00915.36
7.2.220.0100.00815.24
7.2.210.0030.01215.29
7.2.200.0320.00715.15
7.2.190.0080.00815.23
7.2.180.0110.00915.27
7.2.170.0260.00815.27
7.2.160.0130.00715.09
7.2.150.0280.00815.74
7.2.140.0460.00815.81
7.2.130.0520.01115.89
7.2.120.0450.00815.86
7.2.110.0420.00715.68
7.2.100.1010.00715.74
7.2.90.0370.00515.83
7.2.80.0380.00916.15
7.2.70.0580.01015.75
7.2.60.0300.00916.13
7.2.50.0300.00715.70
7.2.40.0550.00715.97
7.2.30.0200.01216.47
7.2.20.0360.00917.16
7.2.10.0340.01117.29
7.2.00.0350.00917.20
7.1.330.0030.01216.06
7.1.320.0300.00715.29
7.1.310.0330.00915.18
7.1.300.0210.00515.25
7.1.290.0420.00715.23
7.1.280.0260.00815.19
7.1.270.0300.00815.23
7.1.260.0320.01115.19
7.1.250.0450.01314.60
7.1.240.0480.00714.76
7.1.230.0380.01114.74
7.1.220.0520.00814.74
7.1.210.0420.00314.66
7.1.200.0400.00714.97
7.1.190.0780.00914.81
7.1.180.0400.00714.75
7.1.170.0470.00714.73
7.1.160.0580.00414.78
7.1.150.0700.01015.14
7.1.140.0480.00916.07
7.1.130.0360.00816.02
7.1.120.0380.01116.12
7.1.110.0390.00715.81
7.1.100.0300.01015.96
7.1.90.0280.00916.31
7.1.80.0310.01016.25
7.1.70.0180.00915.78
7.1.60.0350.01122.91
7.1.50.0290.01525.07
7.1.40.0230.00924.91
7.1.30.0380.00924.95
7.1.20.0350.01124.90
7.1.10.0190.01115.86
7.1.00.0210.01015.98
7.0.330.0850.01113.57
5.6.400.0140.00714.01
5.6.390.0120.01213.83
5.6.380.0140.01114.07
5.6.00.0140.00415.62

preferences:
68.24 ms | 401 KiB | 5 Q