@ 2013-10-11T16:48:12Z <?php
function gen() {
$data = array();
$i = 0;
for($i; $i < 10; $i++) {
$data[$i] = array(
'indent' => 'test ' . $i,
'depth' => 1
);
}
for($i; $i < 690; $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));
*/
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
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).
Version System time (s) User time (s) Memory (MiB) 7.2.10 0.116 0.003 14.97 7.2.9 0.121 0.010 14.88 7.2.8 0.148 0.003 14.69 7.2.7 0.129 0.000 14.96 7.2.6 0.139 0.009 15.45 7.2.5 0.126 0.010 15.33 7.2.4 0.164 0.007 15.42 7.1.22 0.106 0.007 13.85 7.1.21 0.109 0.007 13.97 7.1.20 0.098 0.007 13.94 7.1.19 0.112 0.007 13.91 7.1.18 0.157 0.010 14.20 7.1.17 0.134 0.009 14.24 7.0.31 0.123 0.006 13.77 7.0.30 0.239 0.007 13.76 5.6.38 0.005 0.010 14.93 5.6.37 0.007 0.010 14.96 5.6.36 0.007 0.010 14.91
preferences:dark mode live preview
28.57 ms | 400 KiB | 5 Q