3v4l.org

run code in 300+ PHP versions simultaneously
<?php $values = [ [ 'id' => '2', 'category_id' => 'A', ], [ 'id' => '30', 'category_id' => 'A', ], [ 'id' => '12', 'category_id' => 'B', ], [ 'id' => '8', 'category_id' => 'B', ], [ 'id' => '10', 'category_id' => 'C', ], [ 'id' => '329', 'category_id' => 'D', ], [ 'id' => '89', 'category_id' => 'E', ], [ 'id' => '79', 'category_id' => 'E', ], [ 'id' => '58', 'category_id' => 'A', ], [ 'id' => '219', 'category_id' => 'B', ], [ 'id' => '44', 'category_id' => 'B', ], [ 'id' => '3219', 'category_id' => 'B', ], [ 'id' => '231', 'category_id' => 'B', ], [ 'id' => '43210', 'category_id' => 'F', ] ]; // 一次元配列にする $categoryIds = array_column($values, 'category_id'); // 処理済みの category_id を格納する変数 $sorted = []; // 残っている category_id が一種類のみかどうかを判定する変数 $hasOneSubject = false; $sorting = function ($categoryIds) use (&$sorting, &$sorted, &$hasOneCategory) { // 重複を削除した category_id $uniqueCategory = array_unique($categoryIds); // 使用済みの category_id を格納しておく変数 $used = []; foreach ($categoryIds as $index => $id) { // 処理済みの配列から一番最後を取得 $lastKey = array_key_last($sorted); // 残りの category_id が一種類しかない場合を除き、直近の配列が同じ場合はskip。 if (isset($sorted[$lastKey]) && $sorted[$lastKey] === $id && !$hasOneCategory) { continue; } // category_id を全種類使い切った場合は使用済みをリセットする if (count($uniqueCategory) === count(array_unique($used))) { $used = []; } // 既に同一の category_id を処理済みの場合はskip if (in_array($id, $used)) { continue; } $sorted[$index] = $id; $used[] = $id; } // 残りの配列 $diff = array_diff_assoc($categoryIds, $sorted); // 残りの配列が一種類かどうか $hasOneCategory = count(array_unique($diff)) === 1; // 残っている配列がある場合は再帰処理を実施 if (count($diff) > 0) { return $sorting($diff); } return $sorted; }; $sortedValues = []; // 二次元配列次元配列として入れ直す foreach ($sorting($categoryIds) as $key => $value) { $sortedValues[$key] = $values[$key]; } var_dump($sortedValues);

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.4.20.0070.00319.89
8.4.10.0100.01019.70
8.3.150.0030.00616.80
8.3.140.0110.00716.51
8.3.130.0040.00418.53
8.3.120.0060.00318.99
8.3.110.0030.00520.94
8.3.100.0060.00324.06
8.3.90.0080.00026.77
8.3.80.0060.00316.63
8.3.70.0120.00316.88
8.3.60.0150.00716.50
8.3.50.0070.01119.72
8.3.40.0120.00321.93
8.3.30.0090.00618.88
8.3.20.0050.00324.18
8.3.10.0000.00824.66
8.3.00.0070.00326.16
8.2.270.0120.00618.34
8.2.260.0060.00317.30
8.2.250.0050.00316.68
8.2.240.0030.00617.28
8.2.230.0100.00022.58
8.2.220.0060.00337.54
8.2.210.0040.00426.77
8.2.200.0050.00516.50
8.2.190.0120.00616.75
8.2.180.0040.01125.92
8.2.170.0080.00819.04
8.2.160.0110.00422.96
8.2.150.0040.00425.66
8.2.140.0030.00524.66
8.2.130.0080.00026.16
8.2.120.0040.00419.82
8.2.110.0030.01721.02
8.2.100.0100.00317.91
8.2.90.0030.00517.75
8.2.80.0030.00618.78
8.2.70.0000.00817.63
8.2.60.0040.00418.05
8.2.50.0040.00418.16
8.2.40.0060.00618.05
8.2.30.0060.00317.69
8.2.20.0040.00420.33
8.2.10.0080.00017.82
8.2.00.0040.00419.39
8.1.310.0050.00318.65
8.1.300.0070.00317.88
8.1.290.0090.00018.88
8.1.280.0120.00925.92
8.1.270.0060.00323.99
8.1.260.0050.00326.35
8.1.250.0080.00028.09
8.1.240.0040.00423.86
8.1.230.0120.00022.66
8.1.220.0000.00817.78
8.1.210.0000.00918.77
8.1.200.0060.00317.35
8.1.190.0040.00417.23
8.1.180.0030.00618.10
8.1.170.0000.00817.63
8.1.160.0070.00020.28
8.1.150.0000.00718.52
8.1.140.0040.00417.47
8.1.130.0000.00717.53
8.1.120.0080.00317.46
8.1.110.0000.00817.38
8.1.100.0060.00317.36
8.1.90.0000.00817.43
8.1.80.0050.00517.36
8.1.70.0000.00717.49
8.1.60.0030.00617.49
8.1.50.0060.00317.56
8.1.40.0040.00417.53
8.1.30.0000.00817.63
8.1.20.0080.00017.69
8.1.10.0050.00817.46
8.1.00.0170.00817.39
8.0.300.0060.00320.13
8.0.290.0000.00816.63
8.0.280.0070.00018.46
8.0.270.0000.00716.78
8.0.260.0030.00316.75
8.0.250.0000.00816.88
8.0.240.0060.00316.86
8.0.230.0050.00216.99
8.0.220.0070.00016.82
8.0.210.0030.00516.90
8.0.200.0030.00316.90
8.0.190.0000.00816.96
8.0.180.0050.00216.82
8.0.170.0030.00616.89
8.0.160.0020.00516.85
8.0.150.0050.00216.92
8.0.140.0220.00016.80
8.0.130.0140.00716.89
8.0.120.0170.00616.73
8.0.110.0120.00616.75
8.0.100.0150.00516.82
8.0.90.0150.00616.84
8.0.80.0120.00816.86
8.0.70.0150.00616.80
8.0.60.0120.00816.95
8.0.50.0140.00516.83
8.0.30.0200.00016.75
8.0.20.0180.00317.02
8.0.10.0110.00816.91
8.0.00.0030.00916.90
7.4.330.0000.00515.55
7.4.320.0030.00616.66
7.4.300.0000.00716.60
7.4.290.0080.00016.46
7.4.280.0080.00416.50
7.4.270.0000.01016.60
7.4.260.0180.00516.57
7.4.250.0190.00316.55
7.4.240.0150.00916.54
7.4.230.0180.00616.42
7.4.220.0100.01016.49
7.4.210.0110.00816.48
7.4.200.0130.00516.55
7.4.190.0100.00516.43
7.4.180.0160.00316.57
7.4.160.0120.00816.57
7.4.150.0100.01016.51
7.4.140.0100.01016.53
7.4.130.0170.00316.34
7.4.120.0070.00816.41
7.4.110.0140.00216.42
7.4.100.0160.00416.51
7.4.90.0150.00716.46
7.4.80.0200.00016.42
7.4.70.0070.00716.43
7.4.60.0090.00216.52
7.4.50.0130.00316.27
7.4.40.0120.00716.20
7.4.30.0180.00016.40
7.4.20.0170.00316.37
7.4.10.0080.00516.57
7.4.00.0120.00616.44
7.3.330.0160.00716.36
7.3.320.0130.00616.24
7.3.310.0100.00316.40
7.3.300.0190.00316.22
7.3.290.0160.00616.21
7.3.280.0110.00616.46
7.3.270.0170.00016.45
7.3.260.0170.00316.46
7.3.250.0110.00916.24
7.3.240.0110.00416.40
7.3.230.0110.00816.40
7.3.220.0160.00316.33
7.3.210.0140.00416.16
7.3.200.0110.00816.26
7.3.190.0180.00316.14
7.3.180.0180.00416.16
7.3.170.0130.00816.34
7.3.160.0140.00316.31
7.3.150.0150.00316.16
7.3.140.0090.00816.15
7.3.130.0070.00816.40

preferences:
110.67 ms | 403 KiB | 5 Q