3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * Normalise a flat dot notation array. * * @param array $rows The flat array * @param string $dot The character that separates the layers. * * @return array */ function unflatten(array $rows, string $dot = "."): array { # The last row's main table columns $last_main_table = []; # A collection of all the joined tables belonging to a given row of the main table $joined_tables = []; # For each row in the result set foreach($rows as $id => $row){ # Contains this row's main table columns $main_table = []; # Contains this row's joined table columns $joined_table = []; # Foreach column and value foreach($row as $col => $val){ /** * Whether a column name contains a period or not * is the determining factor of whether the column * belongs to the main table (no period) or a joined * table (periods). */ if(!strpos($col, $dot)){ //if the column belongs to the main table $main_table[$col] = $val; } else { //If the column belongs to a joined table # Break open the column name $col = explode($dot,$col); # Extract out the joined table, and the column names (that may include joined children) $joined_table[array_shift($col)][$id][implode($dot,$col)] = $val; } } # If this is the first row, or if this row's main columns are the same as the last row if(!$last_main_table || ($main_table == $last_main_table)){ # Update the last main table variable $last_main_table = $main_table; # merge all rows of columns belonging to joined tables $joined_tables = array_merge_recursive($joined_tables ?:[], $joined_table ?:[]); # Go to the next row continue; } # At this point, the main columns are different from the previous row's main columns # Add the last row's main columns, merged with all of it's joined table rows to the normalised array $normalised[] = array_merge($last_main_table, $joined_tables); # Update the last main table variable $last_main_table = $main_table; # Reset the joined tables $joined_tables = []; # Add this row's joined table columns $joined_tables = array_merge_recursive($joined_tables ?:[], $joined_table ?:[]); } # Capture the last row $normalised[] = array_merge($last_main_table, $joined_tables); # Go deeper foreach($normalised as $id => $row){ //For each row that is now normalised foreach($row as $key => $val){ //For each column value if(is_array($val)){ //if any of the values are arrays, make sure that array is also normalised $normalised[$id][$key] = unflatten($val, $dot); } } } return $normalised; } $results[] = [ 'id' => 'A1', 'address.id' => '11f456A1', 'address.coordinates.lat' => '12.345', 'address.coordinates.lng' => '67.89', 'address.coordinates.geo.accurate' => true, ]; $results[] = [ 'id' => 'A1', 'address.id' => '22f456A1', 'address.coordinates.lat' => '12.345', 'address.coordinates.lng' => '67.89', 'address.coordinates.geo.accurate' => true, ]; $results[] = [ 'id' => 'A2', 'address.id' => '44f456A2', 'address.coordinates.lat' => '11.345', 'address.coordinates.lng' => '67.89', 'address.coordinates.geo.accurate' => true, ]; $results[] = [ 'id' => 'A2', 'address.id' => '44f456A2', 'address.coordinates.lat' => '22.345', 'address.coordinates.lng' => '67.89', 'address.coordinates.geo.accurate' => true, ]; $normalised = unflatten($results); var_export($normalised);

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.00.0130.00920.01
8.4.150.0030.00014.05
8.4.140.0150.00617.66
8.4.130.0130.00717.71
8.4.120.0110.00720.32
8.4.110.0130.00822.46
8.4.100.0110.01019.24
8.4.90.0070.00518.89
8.4.80.0060.00417.93
8.4.70.0080.00818.11
8.4.60.0120.00918.86
8.4.50.0090.01218.82
8.4.40.0110.00917.63
8.4.30.0030.01318.71
8.4.20.0100.01019.76
8.4.10.0060.00319.35
8.3.280.0110.00920.81
8.3.270.0130.00716.81
8.3.260.0110.00916.59
8.3.250.0030.00518.98
8.3.240.0120.00717.23
8.3.230.0110.00816.82
8.3.220.0110.00717.05
8.3.210.0090.00816.58
8.3.200.0070.00116.79
8.3.190.0140.00717.14
8.3.180.0060.00316.85
8.3.170.0090.00918.89
8.3.160.0090.00916.46
8.3.150.0170.00319.16
8.3.140.0060.00916.75
8.3.130.0030.00616.68
8.3.120.0070.01119.00
8.3.110.0060.00920.94
8.3.100.0090.00624.06
8.3.90.0090.00926.77
8.3.80.0030.00619.36
8.3.70.0110.00718.30
8.3.60.0120.00618.55
8.3.50.0100.00924.49
8.3.40.0120.00318.55
8.3.30.0130.00618.89
8.3.20.0080.00024.18
8.3.10.0050.00324.66
8.3.00.0050.00326.16
8.2.290.0130.00520.25
8.2.280.0100.00818.35
8.2.270.0070.01118.86
8.2.260.0090.00918.38
8.2.250.0000.00816.61
8.2.240.0080.00017.22
8.2.230.0000.00822.58
8.2.220.0090.00037.54
8.2.210.0040.00426.77
8.2.200.0030.00618.88
8.2.190.0180.00416.58
8.2.180.0140.00725.92
8.2.170.0040.01219.01
8.2.160.0140.00022.96
8.2.150.0040.00425.66
8.2.140.0050.00324.66
8.2.130.0050.00226.16
8.2.120.0040.00426.35
8.2.110.0060.00320.52
8.2.100.0060.00617.91
8.2.90.0060.00317.88
8.2.80.0050.00318.53
8.2.70.0000.00817.50
8.2.60.0040.00417.38
8.2.50.0040.00418.05
8.2.40.0000.00818.16
8.2.30.0070.00018.09
8.2.20.0030.00518.05
8.2.10.0040.00419.39
8.2.00.0000.00719.34
8.1.330.0090.01021.98
8.1.320.0070.01115.95
8.1.310.0110.00718.69
8.1.300.0150.00018.57
8.1.290.0060.00330.84
8.1.280.0140.00025.92
8.1.270.0060.00323.99
8.1.260.0000.00728.09
8.1.250.0040.00428.09
8.1.240.0030.00619.13
8.1.230.0070.00417.83
8.1.220.0000.00817.79
8.1.210.0030.00518.77
8.1.200.0040.00417.25
8.1.190.0040.00417.23
8.1.180.0090.00318.10
8.1.170.0040.00418.65
8.1.160.0000.00718.81
8.1.150.0070.00018.95
8.1.140.0000.00718.91
8.1.130.0030.00320.21
8.1.120.0000.00717.54
8.1.110.0000.00717.39
8.1.100.0090.00017.46
8.1.90.0000.00817.43
8.1.80.0080.00017.54
8.1.70.0070.00017.50
8.1.60.0040.00417.59
8.1.50.0030.00517.55
8.1.40.0000.00817.55
8.1.30.0080.00017.54
8.1.20.0000.00817.64
8.1.10.0060.00317.54
8.1.00.0080.00017.41
8.0.300.0040.00420.86
8.0.290.0030.00616.58
8.0.280.0000.00718.38
8.0.270.0000.00717.94
8.0.260.0030.00318.46
8.0.250.0040.00416.99
8.0.240.0040.00416.90
8.0.230.0000.00716.95
8.0.220.0030.00316.93
8.0.210.0040.00416.91
8.0.200.0050.00316.94
8.0.190.0060.00316.89
8.0.180.0040.00416.98
8.0.170.0000.00916.95
8.0.160.0040.00416.98
8.0.150.0000.00816.82
8.0.140.0040.00716.95
8.0.130.0030.00313.36
8.0.120.0030.00516.89
8.0.110.0000.00716.91
8.0.100.0000.00716.80
8.0.90.0000.00716.90
8.0.80.0040.01316.91
8.0.70.0090.00016.76
8.0.60.0000.00816.93
8.0.50.0000.00716.94
8.0.30.0210.00316.96
8.0.20.0140.00517.19
8.0.10.0000.00716.79
8.0.00.0120.00616.80
7.4.330.0050.00015.55
7.4.320.0060.00016.66
7.4.300.0000.00716.48
7.4.290.0000.00816.56
7.4.280.0000.00716.46
7.4.270.0000.00816.51
7.4.260.0060.00013.25
7.4.250.0000.00716.48
7.4.240.0000.00716.66
7.4.230.0000.00716.44
7.4.220.0020.00516.52
7.4.210.0050.01116.65
7.4.200.0040.00416.60
7.4.130.0090.00916.54
7.4.120.0090.00916.45
7.4.110.0000.01716.51
7.4.100.0100.00916.69
7.4.90.0090.00916.65
7.4.80.0140.00619.39
7.4.70.0030.01316.49
7.4.60.0060.01016.20
7.4.50.0140.00316.27
7.4.40.0120.00416.56
7.4.30.0060.01116.42
7.4.20.0070.01016.61
7.4.10.0120.00416.18
7.4.00.0000.01616.48
7.3.330.0030.00616.38
7.3.320.0000.00513.40
7.3.310.0020.00516.44
7.3.300.0080.00016.32
7.3.290.0060.01016.42
7.3.260.0100.01016.47
7.3.240.0090.00916.60
7.3.230.0120.00916.43
7.3.210.0060.00916.39
7.3.200.0030.01416.58
7.3.190.0090.00716.47
7.3.180.0110.00716.30
7.3.170.0040.01216.30
7.3.160.0060.01216.27
7.3.150.0090.00616.38
7.3.140.0130.00316.35
7.3.130.0080.00816.35
7.3.120.0130.00616.24
7.3.110.0140.00316.41
7.3.100.0150.00616.36
7.3.90.0060.01116.25
7.3.80.0130.00316.40
7.3.70.0070.01016.26
7.3.60.0100.00816.34
7.3.50.0100.00716.48
7.3.40.0130.00316.30
7.3.30.0080.00816.27
7.3.20.0110.00616.55
7.3.10.0130.00516.48
7.3.00.0130.00316.37
7.2.330.0130.00416.78
7.2.320.0130.00516.54
7.2.310.0060.01116.37
7.2.300.0170.00016.66
7.2.290.0090.00916.54
7.2.280.0100.00816.76
7.2.270.0140.00416.73
7.2.260.0100.00716.65
7.2.250.0140.00816.46
7.2.240.0100.01316.66
7.2.230.0080.00816.92
7.2.220.0040.01316.52
7.2.210.0080.00816.50
7.2.200.0050.01116.61
7.2.190.0070.01116.70
7.2.180.0110.00716.50
7.2.170.0110.00616.55
7.2.160.0070.01016.55
7.2.150.0090.00916.84
7.2.140.0030.01616.77
7.2.130.0120.00616.59
7.2.120.0190.00816.65
7.2.110.0080.01116.55
7.2.100.0130.00616.61
7.2.90.0060.01216.41
7.2.80.0070.01716.50
7.2.70.0140.00416.62
7.2.60.0180.00016.70
7.2.50.0070.01016.62
7.2.40.0170.00916.63
7.2.30.0090.00916.61
7.2.20.0060.01216.67
7.2.10.0150.00316.82
7.2.00.0030.01516.51

preferences:
164.69 ms | 403 KiB | 5 Q