3v4l.org

run code in 300+ PHP versions simultaneously
<?php function buildTreeHelper(&$array, $currentLevel = 1) { $result = array(); $lastIndex = 0; while($pair = each($array)) { list(, $row) = $pair; $level = $row['level']; if ($level > $currentLevel) { $result[$lastIndex]['children'] = buildTreeHelper($array, $level); } else if ($level == $currentLevel) { $result[++$lastIndex] = $row; } else { prev($array); // shift back break; } } return $result; } function buildTree($array) { reset($array); return buildTreeHelper($array); } $array = [ ['level'=>1, 'name' => 'Root #1'], ['level'=>1, 'name' => 'Root #2'], ['level'=>2, 'name' => 'subroot 2-1'], ['level'=>3, 'name' => '__subroot 2-1/1'], ['level'=>3, 'name' => '__subroot 2-1/2'], ['level'=>4, 'name' => '__subroot 2-2/1'], ['level'=>2, 'name' => 'subroot 2-2'], ['level'=>2, 'name' => 'subroot 2-3'], ['level'=>1, 'name' => 'Root #3'] ]; $time = microtime(true); echo(buildTree($array)); echo round(microtime(true) - $time, 10);
Output for 7.4.1
Deprecated: The each() function is deprecated. This message will be suppressed on further calls in /in/2pbDR on line 7 Notice: Array to string conversion in /in/2pbDR on line 42 Array6.79493E-5
Output for 7.4.0
Deprecated: The each() function is deprecated. This message will be suppressed on further calls in /in/2pbDR on line 7 Notice: Array to string conversion in /in/2pbDR on line 42 Array9.10759E-5
Output for 7.3.13
Deprecated: The each() function is deprecated. This message will be suppressed on further calls in /in/2pbDR on line 7 Notice: Array to string conversion in /in/2pbDR on line 42 Array7.00951E-5
Output for 7.3.12
Deprecated: The each() function is deprecated. This message will be suppressed on further calls in /in/2pbDR on line 7 Notice: Array to string conversion in /in/2pbDR on line 42 Array8.98838E-5
Output for 7.3.11
Deprecated: The each() function is deprecated. This message will be suppressed on further calls in /in/2pbDR on line 7 Notice: Array to string conversion in /in/2pbDR on line 42 Array8.39233E-5
Output for 7.3.10
Deprecated: The each() function is deprecated. This message will be suppressed on further calls in /in/2pbDR on line 7 Notice: Array to string conversion in /in/2pbDR on line 42 Array5.6982E-5
Output for 7.2.3, 7.3.9
Deprecated: The each() function is deprecated. This message will be suppressed on further calls in /in/2pbDR on line 7 Notice: Array to string conversion in /in/2pbDR on line 42 Array6.41346E-5
Output for 7.3.8
Deprecated: The each() function is deprecated. This message will be suppressed on further calls in /in/2pbDR on line 7 Notice: Array to string conversion in /in/2pbDR on line 42 Array5.60284E-5
Output for 7.3.7
Deprecated: The each() function is deprecated. This message will be suppressed on further calls in /in/2pbDR on line 7 Notice: Array to string conversion in /in/2pbDR on line 42 Array8.29697E-5
Output for 7.3.6
Deprecated: The each() function is deprecated. This message will be suppressed on further calls in /in/2pbDR on line 7 Notice: Array to string conversion in /in/2pbDR on line 42 Array7.79629E-5
Output for 7.2.12, 7.3.5
Deprecated: The each() function is deprecated. This message will be suppressed on further calls in /in/2pbDR on line 7 Notice: Array to string conversion in /in/2pbDR on line 42 Array5.38826E-5
Output for 7.3.4
Deprecated: The each() function is deprecated. This message will be suppressed on further calls in /in/2pbDR on line 7 Notice: Array to string conversion in /in/2pbDR on line 42 Array0.0001018047
Output for 7.3.3
Deprecated: The each() function is deprecated. This message will be suppressed on further calls in /in/2pbDR on line 7 Notice: Array to string conversion in /in/2pbDR on line 42 Array5.10216E-5
Output for 7.2.0, 7.2.20, 7.3.2
Deprecated: The each() function is deprecated. This message will be suppressed on further calls in /in/2pbDR on line 7 Notice: Array to string conversion in /in/2pbDR on line 42 Array5.50747E-5
Output for 7.2.1, 7.2.5, 7.3.1
Deprecated: The each() function is deprecated. This message will be suppressed on further calls in /in/2pbDR on line 7 Notice: Array to string conversion in /in/2pbDR on line 42 Array8.70228E-5
Output for 7.3.0
Deprecated: The each() function is deprecated. This message will be suppressed on further calls in /in/2pbDR on line 7 Notice: Array to string conversion in /in/2pbDR on line 42 Array7.9155E-5
Output for 7.2.26
Deprecated: The each() function is deprecated. This message will be suppressed on further calls in /in/2pbDR on line 7 Notice: Array to string conversion in /in/2pbDR on line 42 Array9.48906E-5
Output for 7.2.25
Deprecated: The each() function is deprecated. This message will be suppressed on further calls in /in/2pbDR on line 7 Notice: Array to string conversion in /in/2pbDR on line 42 Array8.01086E-5
Output for 7.2.13 - 7.2.14, 7.2.24
Deprecated: The each() function is deprecated. This message will be suppressed on further calls in /in/2pbDR on line 7 Notice: Array to string conversion in /in/2pbDR on line 42 Array5.88894E-5
Output for 7.2.23
Deprecated: The each() function is deprecated. This message will be suppressed on further calls in /in/2pbDR on line 7 Notice: Array to string conversion in /in/2pbDR on line 42 Array6.29425E-5
Output for 7.2.22
Deprecated: The each() function is deprecated. This message will be suppressed on further calls in /in/2pbDR on line 7 Notice: Array to string conversion in /in/2pbDR on line 42 Array6.10352E-5
Output for 7.2.21
Deprecated: The each() function is deprecated. This message will be suppressed on further calls in /in/2pbDR on line 7 Notice: Array to string conversion in /in/2pbDR on line 42 Array7.48634E-5
Output for 7.2.7, 7.2.19
Deprecated: The each() function is deprecated. This message will be suppressed on further calls in /in/2pbDR on line 7 Notice: Array to string conversion in /in/2pbDR on line 42 Array7.29561E-5
Output for 7.2.18
Deprecated: The each() function is deprecated. This message will be suppressed on further calls in /in/2pbDR on line 7 Notice: Array to string conversion in /in/2pbDR on line 42 Array6.8903E-5
Output for 7.2.17
Deprecated: The each() function is deprecated. This message will be suppressed on further calls in /in/2pbDR on line 7 Notice: Array to string conversion in /in/2pbDR on line 42 Array6.69956E-5
Output for 7.2.16
Deprecated: The each() function is deprecated. This message will be suppressed on further calls in /in/2pbDR on line 7 Notice: Array to string conversion in /in/2pbDR on line 42 Array0.0001089573
Output for 7.2.15
Deprecated: The each() function is deprecated. This message will be suppressed on further calls in /in/2pbDR on line 7 Notice: Array to string conversion in /in/2pbDR on line 42 Array6.81877E-5
Output for 7.2.8, 7.2.11
Deprecated: The each() function is deprecated. This message will be suppressed on further calls in /in/2pbDR on line 7 Notice: Array to string conversion in /in/2pbDR on line 42 Array8.79765E-5
Output for 7.2.10
Deprecated: The each() function is deprecated. This message will be suppressed on further calls in /in/2pbDR on line 7 Notice: Array to string conversion in /in/2pbDR on line 42 Array7.60555E-5
Output for 7.2.9
Deprecated: The each() function is deprecated. This message will be suppressed on further calls in /in/2pbDR on line 7 Notice: Array to string conversion in /in/2pbDR on line 42 Array8.60691E-5
Output for 7.2.6
Deprecated: The each() function is deprecated. This message will be suppressed on further calls in /in/2pbDR on line 7 Notice: Array to string conversion in /in/2pbDR on line 42 Array7.89165E-5
Output for 7.2.4
Deprecated: The each() function is deprecated. This message will be suppressed on further calls in /in/2pbDR on line 7 Notice: Array to string conversion in /in/2pbDR on line 42 Array8.10623E-5
Output for 7.2.2
Deprecated: The each() function is deprecated. This message will be suppressed on further calls in /in/2pbDR on line 7 Notice: Array to string conversion in /in/2pbDR on line 42 Array8.58307E-5
Output for 5.4.10, 5.5.8, 5.5.20, 5.6.6, 5.6.19, 7.0.6, 7.1.33
Notice: Array to string conversion in /in/2pbDR on line 42 Array6.69956E-5
Output for 7.0.21, 7.1.32
Notice: Array to string conversion in /in/2pbDR on line 42 Array5.00679E-5
Output for 7.1.1, 7.1.31
Notice: Array to string conversion in /in/2pbDR on line 42 Array4.79221E-5
Output for 7.0.20, 7.0.32, 7.1.26, 7.1.30
Notice: Array to string conversion in /in/2pbDR on line 42 Array4.69685E-5
Output for 7.0.0, 7.0.27, 7.1.29
Notice: Array to string conversion in /in/2pbDR on line 42 Array4.50611E-5
Output for 7.0.12, 7.1.5, 7.1.28
Notice: Array to string conversion in /in/2pbDR on line 42 Array5.10216E-5
Output for 7.1.27
Notice: Array to string conversion in /in/2pbDR on line 42 Array8.41618E-5
Output for 5.5.28, 5.6.31, 7.1.25
Notice: Array to string conversion in /in/2pbDR on line 42 Array6.60419E-5
Output for 5.4.17, 5.6.15, 7.1.17, 7.1.24
Notice: Array to string conversion in /in/2pbDR on line 42 Array5.60284E-5
Output for 7.1.13, 7.1.23
Notice: Array to string conversion in /in/2pbDR on line 42 Array5.19753E-5
Output for 5.4.28, 7.1.22
Notice: Array to string conversion in /in/2pbDR on line 42 Array8.51154E-5
Output for 7.1.21
Notice: Array to string conversion in /in/2pbDR on line 42 Array4.91142E-5
Output for 5.5.1, 5.5.6, 7.0.14, 7.0.23, 7.1.20
Notice: Array to string conversion in /in/2pbDR on line 42 Array5.88894E-5
Output for 5.6.7, 5.6.23, 7.1.19
Notice: Array to string conversion in /in/2pbDR on line 42 Array7.20024E-5
Output for 5.6.9, 5.6.16, 7.0.4, 7.1.18
Notice: Array to string conversion in /in/2pbDR on line 42 Array6.10352E-5
Output for 7.0.8, 7.1.16
Notice: Array to string conversion in /in/2pbDR on line 42 Array5.29289E-5
Output for 5.6.0, 5.6.22, 7.1.15
Notice: Array to string conversion in /in/2pbDR on line 42 Array6.19888E-5
Output for 7.1.0, 7.1.14
Notice: Array to string conversion in /in/2pbDR on line 42 Array4.29153E-5
Output for 5.5.11, 5.6.8, 5.6.13, 7.0.13, 7.0.18, 7.1.12
Notice: Array to string conversion in /in/2pbDR on line 42 Array5.91278E-5
Output for 5.4.4, 5.6.4, 7.0.1, 7.0.26, 7.1.11
Notice: Array to string conversion in /in/2pbDR on line 42 Array6.29425E-5
Output for 5.4.1, 7.0.31, 7.1.10
Notice: Array to string conversion in /in/2pbDR on line 42 Array7.60555E-5
Output for 5.4.35, 5.4.42, 7.1.9
Notice: Array to string conversion in /in/2pbDR on line 42 Array8.2016E-5
Output for 5.4.26, 5.5.31, 5.6.40, 7.1.8
Notice: Array to string conversion in /in/2pbDR on line 42 Array6.8903E-5
Output for 7.0.28, 7.1.7
Notice: Array to string conversion in /in/2pbDR on line 42 Array4.81606E-5
Output for 5.4.31, 5.5.5, 5.5.26, 7.1.6
Notice: Array to string conversion in /in/2pbDR on line 42 Array7.70092E-5
Output for 5.6.21, 7.1.3 - 7.1.4
Notice: Array to string conversion in /in/2pbDR on line 42 Array6.79493E-5
Output for 7.1.2
Notice: Array to string conversion in /in/2pbDR on line 42 Array6.07967E-5
Output for 5.4.37, 5.6.30, 7.0.33
Notice: Array to string conversion in /in/2pbDR on line 42 Array6.41346E-5
Output for 7.0.30
Notice: Array to string conversion in /in/2pbDR on line 42 Array5.31673E-5
Output for 7.0.29
Notice: Array to string conversion in /in/2pbDR on line 42 Array4.88758E-5
Output for 7.0.9 - 7.0.10, 7.0.25
Notice: Array to string conversion in /in/2pbDR on line 42 Array4.31538E-5
Output for 7.0.24
Notice: Array to string conversion in /in/2pbDR on line 42 Array9.10759E-5
Output for 5.4.41, 5.6.35, 7.0.11, 7.0.22
Notice: Array to string conversion in /in/2pbDR on line 42 Array8.39233E-5
Output for 5.6.25, 7.0.16, 7.0.19
Notice: Array to string conversion in /in/2pbDR on line 42 Array6.50883E-5
Output for 5.4.32, 5.6.38, 7.0.17
Notice: Array to string conversion in /in/2pbDR on line 42 Array8.60691E-5
Output for 5.4.16, 5.4.18, 5.4.40, 5.5.22, 5.5.34, 5.6.3, 7.0.15
Notice: Array to string conversion in /in/2pbDR on line 42 Array9.20296E-5
Output for 7.0.7
Notice: Array to string conversion in /in/2pbDR on line 42 Array5.4121E-5
Output for 5.5.21, 7.0.5
Notice: Array to string conversion in /in/2pbDR on line 42 Array6.48499E-5
Output for 5.5.16, 5.5.33, 5.6.26, 7.0.3
Notice: Array to string conversion in /in/2pbDR on line 42 Array7.79629E-5
Output for 7.0.2
Notice: Array to string conversion in /in/2pbDR on line 42 Array7.51019E-5
Output for 5.6.39
Notice: Array to string conversion in /in/2pbDR on line 42 Array9.98974E-5
Output for 5.4.34, 5.5.13, 5.6.12, 5.6.33, 5.6.37
Notice: Array to string conversion in /in/2pbDR on line 42 Array6.38962E-5
Output for 5.6.29, 5.6.36
Notice: Array to string conversion in /in/2pbDR on line 42 Array6.81877E-5
Output for 5.5.15, 5.6.11, 5.6.34
Notice: Array to string conversion in /in/2pbDR on line 42 Array8.89301E-5
Output for 5.6.32
Notice: Array to string conversion in /in/2pbDR on line 42 Array9.799E-5
Output for 5.6.28
Notice: Array to string conversion in /in/2pbDR on line 42 Array7.98702E-5
Output for 5.4.33, 5.6.5, 5.6.27
Notice: Array to string conversion in /in/2pbDR on line 42 Array7.31945E-5
Output for 5.4.21, 5.5.0, 5.5.24, 5.6.24
Notice: Array to string conversion in /in/2pbDR on line 42 Array8.29697E-5
Output for 5.4.19, 5.6.20
Notice: Array to string conversion in /in/2pbDR on line 42 Array0.0001111031
Output for 5.6.18
Notice: Array to string conversion in /in/2pbDR on line 42 Array7.00951E-5
Output for 5.5.29, 5.6.17
Notice: Array to string conversion in /in/2pbDR on line 42 Array8.01086E-5
Output for 5.6.14
Notice: Array to string conversion in /in/2pbDR on line 42 Array0.0001609325
Output for 5.4.29, 5.6.10
Notice: Array to string conversion in /in/2pbDR on line 42 Array9.01222E-5
Output for 5.6.2
Notice: Array to string conversion in /in/2pbDR on line 42 Array8.79765E-5
Output for 5.6.1
Notice: Array to string conversion in /in/2pbDR on line 42 Array0.0001180172
Output for 5.5.38
Notice: Array to string conversion in /in/2pbDR on line 42 Array6.91414E-5
Output for 5.5.37
Notice: Array to string conversion in /in/2pbDR on line 42 Array0.0001349449
Output for 5.5.18, 5.5.36
Notice: Array to string conversion in /in/2pbDR on line 42 Array7.89165E-5
Output for 5.5.35
Notice: Array to string conversion in /in/2pbDR on line 42 Array0.0001490116
Output for 5.5.32
Notice: Array to string conversion in /in/2pbDR on line 42 Array0.0001149178
Output for 5.5.4, 5.5.23, 5.5.30
Notice: Array to string conversion in /in/2pbDR on line 42 Array6.00815E-5
Output for 5.5.27
Notice: Array to string conversion in /in/2pbDR on line 42 Array9.82285E-5
Output for 5.4.2, 5.4.22, 5.4.44, 5.5.25
Notice: Array to string conversion in /in/2pbDR on line 42 Array0.0001020432
Output for 5.5.19
Notice: Array to string conversion in /in/2pbDR on line 42 Array0.0001029968
Output for 5.5.17
Notice: Array to string conversion in /in/2pbDR on line 42 Array9.5129E-5
Output for 5.5.9, 5.5.14
Notice: Array to string conversion in /in/2pbDR on line 42 Array5.81741E-5
Output for 5.5.12
Notice: Array to string conversion in /in/2pbDR on line 42 Array0.0001130104
Output for 5.5.10
Notice: Array to string conversion in /in/2pbDR on line 42 Array8.98838E-5
Output for 5.5.7
Notice: Array to string conversion in /in/2pbDR on line 42 Array9.17912E-5
Output for 5.5.3
Notice: Array to string conversion in /in/2pbDR on line 42 Array6.98566E-5
Output for 5.5.2
Notice: Array to string conversion in /in/2pbDR on line 42 Array7.9155E-5
Output for 5.4.45
Notice: Array to string conversion in /in/2pbDR on line 42 Array0.0001008511
Output for 5.4.25, 5.4.43
Notice: Array to string conversion in /in/2pbDR on line 42 Array9.41753E-5
Output for 5.4.39
Notice: Array to string conversion in /in/2pbDR on line 42 Array0.000123024
Output for 5.4.5, 5.4.14, 5.4.38
Notice: Array to string conversion in /in/2pbDR on line 42 Array8.70228E-5
Output for 5.4.36
Notice: Array to string conversion in /in/2pbDR on line 42 Array0.0001041889
Output for 5.4.27, 5.4.30
Notice: Array to string conversion in /in/2pbDR on line 42 Array9.70364E-5
Output for 5.4.24
Notice: Array to string conversion in /in/2pbDR on line 42 Array0.0001449585
Output for 5.4.23
Notice: Array to string conversion in /in/2pbDR on line 42 Array0.0001120567
Output for 5.4.20
Notice: Array to string conversion in /in/2pbDR on line 42 Array0.0001010895
Output for 5.4.15
Notice: Array to string conversion in /in/2pbDR on line 42 Array9.29832E-5
Output for 5.4.7, 5.4.13
Notice: Array to string conversion in /in/2pbDR on line 42 Array0.0001199245
Output for 5.4.12
Notice: Array to string conversion in /in/2pbDR on line 42 Array5.79357E-5
Output for 5.4.11
Notice: Array to string conversion in /in/2pbDR on line 42 Array9.08375E-5
Output for 5.4.9
Notice: Array to string conversion in /in/2pbDR on line 42 Array0.0001330376
Output for 5.4.8
Notice: Array to string conversion in /in/2pbDR on line 42 Array0.0001261234
Output for 5.4.6
Notice: Array to string conversion in /in/2pbDR on line 42 Array0.0001060963
Output for 5.4.3
Notice: Array to string conversion in /in/2pbDR on line 42 Array7.29561E-5
Output for 5.4.0
Notice: Array to string conversion in /in/2pbDR on line 42 Array0.0001308918
Output for 4.4.2 - 4.4.9, 5.1.0 - 5.1.6, 5.2.0 - 5.2.17, 5.3.0 - 5.3.29
Parse error: syntax error, unexpected '[' in /in/2pbDR on line 29
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.3.11, 4.4.0 - 4.4.1, 5.0.0 - 5.0.5
Parse error: parse error, unexpected '[' in /in/2pbDR on line 29
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error in /in/2pbDR on line 29
Process exited with code 255.

preferences:
219.53 ms | 401 KiB | 325 Q