3v4l.org

run code in 300+ PHP versions simultaneously
<?php array ( 0 => array ( 'id' => 'users', 'title' => 'Users', 'items' => array ( 0 => array ( 'id' => 'user_1', ), 1 => array ( 'id' => 'user_1', ), ), ), 1 => array ( 'id' => 'players', 'title' => 'Players', 'items' => array ( 0 => array ( 'id' => 'player_1', ), ), ), 2 => array ( 'id' => 'employees', 'title' => 'Employees', 'items' => array ( 0 => array ( 'id' => 'employees_1', ), 1 => array ( 'id' => 'employees_2', ), ), ), 3 => array ( 'id' => 'programmers', 'title' => 'Programmers', 'items' => array ( 0 => array ( 'id' => 'programmer_1', ), 1 => array ( 'id' => 'programmer_2', ), ), ), ); class ArrayCombiner { private $data; public function __construct(array $data) { $this->data = $data; } public function combineAndMerge(array $idsToCombine, $newId, $newTitle) { // Find indices of $idsToCombine in $this->data $indices = $this->findIndicesByIds($idsToCombine); if (count($indices) !== count($idsToCombine)) { return false; // Not all elements were found } // Extract items arrays from $indices $mergedItems = []; foreach ($indices as $index) { $mergedItems = array_merge($mergedItems, $this->data[$index]['items']); } // Create new combined element $combinedElement = [ 'id' => $newId, 'title' => $newTitle, 'items' => $mergedItems, ]; // Remove elements from $this->data based on $indices foreach ($indices as $index) { unset($this->data[$index]); } // Add $combinedElement to $this->data $this->data[] = $combinedElement; // Reindex the array $this->data = array_values($this->data); return true; } public function getData() { return $this->data; } private function findIndicesByIds(array $ids) { $indices = []; foreach ($this->data as $index => $element) { if (in_array($element['id'], $ids)) { $indices[] = $index; } } return $indices; } } // Example usage: $data = [ [ 'id' => 'users', 'title' => 'Users', 'items' => [ ['id' => 'user_1'], ['id' => 'user_2'], ], ], [ 'id' => 'players', 'title' => 'Players', 'items' => [ ['id' => 'player_1'], ], ], [ 'id' => 'employees', 'title' => 'Employees', 'items' => [ ['id' => 'employee_1'], ['id' => 'employee_2'], ], ], [ 'id' => 'programmers', 'title' => 'Programmers', 'items' => [ ['id' => 'programmer_1'], ['id' => 'programmer_2'], ], ], ]; $combiner = new ArrayCombiner($data); // Combine 'users', 'players', 'employees', and 'programmers' into 'combined_group' $combiner->combineAndMerge(['users', 'players', 'employees', 'programmers'], 'combined_group', 'Combined Group'); // Get the modified data $modifiedData = $combiner->getData(); // Display the modified data echo "<pre>"; print_r($modifiedData); echo "</pre>";

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.5.10.0050.01216.42
8.5.00.0100.01420.21
8.4.160.0140.01020.62
8.4.150.0010.00014.05
8.4.140.0140.00717.71
8.4.130.0120.00818.65
8.4.120.0120.00923.83
8.4.110.0120.00919.09
8.4.100.0120.00718.70
8.4.90.0120.00822.07
8.4.80.0120.00920.69
8.4.70.0080.01218.07
8.4.60.0270.01018.13
8.4.50.0290.00618.29
8.4.40.0200.01117.52
8.4.30.0220.00918.24
8.4.20.0230.01119.53
8.4.10.0220.00718.44
8.3.280.0090.01018.46
8.3.270.0110.00816.55
8.3.260.0130.00616.63
8.3.250.0110.00818.79
8.3.240.0120.00817.15
8.3.230.0060.00216.60
8.3.220.0090.00919.01
8.3.210.0080.00716.70
8.3.200.0240.00416.63
8.3.190.0190.00716.87
8.3.180.0190.00816.73
8.3.170.0210.00918.80
8.3.160.0260.00916.51
8.3.150.0210.00916.86
8.3.140.0210.00716.67
8.3.130.0160.01417.48
8.3.120.0170.00818.57
8.3.110.0210.01018.83
8.3.100.0230.00516.65
8.3.90.0270.00517.41
8.3.80.0400.00823.61
8.3.70.0410.00823.71
8.3.60.0300.00723.68
8.3.50.0200.00823.72
8.3.40.0390.00724.12
8.3.30.0400.00324.11
8.3.20.0330.00724.24
8.3.10.0340.00724.24
8.3.00.0270.00824.24
8.2.290.0130.00620.56
8.2.280.0200.00917.62
8.2.270.0220.00817.10
8.2.260.0230.00616.46
8.2.250.0190.01117.44
8.2.240.0210.00916.69
8.2.230.0210.00919.43
8.2.220.0180.00720.34
8.2.210.0150.00921.52
8.2.200.0320.00923.59
8.2.190.0280.01223.65
8.2.180.0340.00823.63
8.2.170.0360.00724.37
8.2.160.0350.00624.33
8.2.150.0360.00824.28
8.2.140.0360.01024.30
8.2.130.0350.01024.18
8.2.120.0350.00524.33
8.2.110.0290.01124.27
8.2.100.0340.00524.24
8.2.90.0330.00424.35
8.2.80.0320.00824.25
8.2.70.0230.00824.11
8.2.60.0340.01024.18
8.2.50.0310.01024.21
8.2.40.0300.01024.26
8.2.30.0320.00724.24
8.2.20.0270.00724.20
8.2.10.0270.00624.28
8.2.00.0340.00524.20
8.1.330.0110.00921.97
8.1.320.0240.00817.28
8.1.310.0180.00816.48
8.1.300.0210.00818.26
8.1.290.0290.01423.52
8.1.280.0300.00923.58
8.1.270.0330.01124.03
8.1.260.0350.00723.99
8.1.250.0350.00424.08
8.1.240.0330.00524.17
8.1.230.0360.00624.15
8.1.220.0310.00724.19
8.1.210.0310.00624.17
8.1.200.0280.01023.90
8.1.190.0280.01024.01
8.1.180.0300.01023.96
8.1.170.0310.00924.09
8.1.160.0270.00924.05
8.1.150.0340.00524.16
8.1.140.0320.00424.05
8.1.130.0260.00424.07
8.1.120.0250.00923.88
8.1.110.0160.01224.06
8.1.100.0190.00724.12
8.1.90.0210.00724.11
8.1.80.0190.01023.96
8.1.70.0200.00724.07
8.1.60.0190.00824.15
8.1.50.0160.01224.13
8.1.40.0270.00424.07
8.1.30.0180.00924.23
8.1.20.0250.00324.21
8.1.10.0230.00724.12
8.1.00.0230.01024.06

preferences:
30.99 ms | 403 KiB | 5 Q