3v4l.org

run code in 300+ PHP versions simultaneously
<?php $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, ) ); 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 find_by_key($key, $array) { foreach($array as $k => &$v) { if($key == $k) { return &$v; } } foreach($array as $sub) { if(isset($sub['children']) && FALSE !== ($found = find_by_key($key, $sub['children']))) { return &$found; } } return FALSE; } function v2($array) { $out = array(); foreach ($array as $key => $sub) { if(!isset($sub['parent_id'])) { $out[$key] = $sub; } else { $parent = find_by_key($sub['parent_id'], $out); $parent['children'][$key] = $sub; } } 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)
5.4.200.0120.04612.40
5.4.190.0180.04312.39
5.4.180.0120.04612.39
5.4.170.0110.04712.40
5.4.160.0160.07112.40
5.4.150.0130.04412.39
5.4.140.0110.04812.09
5.4.130.0100.04612.07
5.4.120.0130.04512.03
5.4.110.0090.04512.03
5.4.100.0110.04612.03
5.4.90.0120.04712.03
5.4.80.0220.06712.03
5.4.70.0100.04312.02
5.4.60.0110.03912.03
5.4.50.0100.04112.02
5.4.40.0100.03912.01
5.4.30.0110.03912.01
5.4.20.0100.03912.00
5.4.10.0090.04212.00
5.4.00.0120.03811.50
5.3.270.0100.04312.72
5.3.260.0110.04312.72
5.3.250.0130.03912.72
5.3.240.0220.06512.72
5.3.230.0100.04612.71
5.3.220.0140.04012.68
5.3.210.0160.05312.68
5.3.200.0110.04112.68
5.3.190.0140.03912.68
5.3.180.0130.04512.68
5.3.170.0110.04512.67
5.3.160.0180.05512.67
5.3.150.0150.04312.67
5.3.140.0120.04712.66
5.3.130.0130.05012.66
5.3.120.0120.04712.66
5.3.110.0190.05012.66
5.3.100.0130.03812.12
5.3.90.0090.04212.11
5.3.80.0110.03912.09
5.3.70.0210.04612.09
5.3.60.0120.03912.07
5.3.50.0120.03812.02
5.3.40.0130.03912.02
5.3.30.0110.03911.99
5.3.20.0130.03611.76
5.3.10.0130.03811.73
5.3.00.0100.03911.71

preferences:
138.64 ms | 1394 KiB | 7 Q