3v4l.org

run code in 300+ PHP versions simultaneously
<?php function gen() { $data = array(); $i = 0; for($i; $i < 10; $i++) { $data[$i] = array( 'indent' => 'test ' . $i, 'depth' => 1 ); } for($i; $i < 700; $i++) { $data[$i] = array( 'indent' => 'test ' . $i, 'depth' => 'x', 'parent_id' => rand(0, $i) ); } return $data; } echo serialize(gen()); /* $array = array( 55 => array( 'ident' => 'test 1', 'depth' => 1, ), 77 => array( 'parent_id' => 55, 'ident' => 'test 2', 'depth' => 2, ), 109 => array( 'parent_id' => 77, 'ident' => 'test 3', 'depth' => 3, ), 78 => array( 'parent_id' => 55, 'ident' => 'test 4', 'depth' => 2, ), 25 => array( 'ident' => 'test 5', 'depth' => 1, ) ); */ $array = unserialize('a:0:{}'); /* function v1($array) { foreach ($array as $key => &$sub) { if (isset($sub['parent_id'])) { $array[$sub['parent_id']]['children'][$key] = &$sub; } } unset($sub); // unset the reference to make sure to not overwrite it later... // now remove the entries with parents foreach ($array as $key => $sub) { if (isset($sub['parent_id'])) unset($array[$key]); } return $array; } function push_at_key($what, $what_key, $key, $array) { foreach($array as $k => $v) { if($key == $k) { $array[$k]['children'][$what_key] = $what; return $array; } } foreach($array as $k => $v) { $array[$k] = push_at_key($what, $what_key, $key, $v['children']); } return $array; } function v2($array) { $out = array(); foreach ($array as $key => $sub) { if(!isset($sub['parent_id'])) { $out[$key] = $sub; } else { $out = push_at_key($sub, $key, $sub['parent_id'], $out); } } return $out; } //var_dump(v1($array)); //var_dump(v2($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)
7.2.100.0160.00615.20
7.2.90.1030.00315.15
7.2.80.1310.01015.30
7.2.70.1090.00315.35
7.2.60.1110.01015.26
7.2.50.1180.00715.47
7.2.40.1630.00015.32
7.1.220.1170.00314.14
7.1.210.0950.00614.09
7.1.200.1280.01014.34
7.1.190.1210.00314.04
7.1.180.1320.01014.21
7.1.170.0260.01314.21
7.0.310.1230.00613.77
7.0.300.1040.00713.79
5.6.380.0130.00715.23
5.6.370.0080.00815.41
5.6.360.0050.00815.25

preferences:
37.95 ms | 401 KiB | 5 Q