@ 2013-10-11T16:47:53Z <?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));
*/
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.016 0.006 15.20 7.2.9 0.103 0.003 15.15 7.2.8 0.131 0.010 15.30 7.2.7 0.109 0.003 15.35 7.2.6 0.111 0.010 15.26 7.2.5 0.118 0.007 15.47 7.2.4 0.163 0.000 15.32 7.1.22 0.117 0.003 14.14 7.1.21 0.095 0.006 14.09 7.1.20 0.128 0.010 14.34 7.1.19 0.121 0.003 14.04 7.1.18 0.132 0.010 14.21 7.1.17 0.026 0.013 14.21 7.0.31 0.123 0.006 13.77 7.0.30 0.104 0.007 13.79 5.6.38 0.013 0.007 15.23 5.6.37 0.008 0.008 15.41 5.6.36 0.005 0.008 15.25
preferences:dark mode live preview
37.95 ms | 401 KiB | 5 Q