3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php function gen() { $data = array(); $i = 0; for($i; $i < 50; $i++) { $data[$i] = array( 'indent' => 'test ' . $i, 'depth' => 1 ); } 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)); */
Output for 4.3.0 - 7.1.0
a:50:{i:0;a:2:{s:6:"indent";s:6:"test 0";s:5:"depth";i:1;}i:1;a:2:{s:6:"indent";s:6:"test 1";s:5:"depth";i:1;}i:2;a:2:{s:6:"indent";s:6:"test 2";s:5:"depth";i:1;}i:3;a:2:{s:6:"indent";s:6:"test 3";s:5:"depth";i:1;}i:4;a:2:{s:6:"indent";s:6:"test 4";s:5:"depth";i:1;}i:5;a:2:{s:6:"indent";s:6:"test 5";s:5:"depth";i:1;}i:6;a:2:{s:6:"indent";s:6:"test 6";s:5:"depth";i:1;}i:7;a:2:{s:6:"indent";s:6:"test 7";s:5:"depth";i:1;}i:8;a:2:{s:6:"indent";s:6:"test 8";s:5:"depth";i:1;}i:9;a:2:{s:6:"indent";s:6:"test 9";s:5:"depth";i:1;}i:10;a:2:{s:6:"indent";s:7:"test 10";s:5:"depth";i:1;}i:11;a:2:{s:6:"indent";s:7:"test 11";s:5:"depth";i:1;}i:12;a:2:{s:6:"indent";s:7:"test 12";s:5:"depth";i:1;}i:13;a:2:{s:6:"indent";s:7:"test 13";s:5:"depth";i:1;}i:14;a:2:{s:6:"indent";s:7:"test 14";s:5:"depth";i:1;}i:15;a:2:{s:6:"indent";s:7:"test 15";s:5:"depth";i:1;}i:16;a:2:{s:6:"indent";s:7:"test 16";s:5:"depth";i:1;}i:17;a:2:{s:6:"indent";s:7:"test 17";s:5:"depth";i:1;}i:18;a:2:{s:6:"indent";s:7:"test 18";s:5:"depth";i:1;}i:19;a:2:{s:6:"indent";s:7:"test 19";s:5:"depth";i:1;}i:20;a:2:{s:6:"indent";s:7:"test 20";s:5:"depth";i:1;}i:21;a:2:{s:6:"indent";s:7:"test 21";s:5:"depth";i:1;}i:22;a:2:{s:6:"indent";s:7:"test 22";s:5:"depth";i:1;}i:23;a:2:{s:6:"indent";s:7:"test 23";s:5:"depth";i:1;}i:24;a:2:{s:6:"indent";s:7:"test 24";s:5:"depth";i:1;}i:25;a:2:{s:6:"indent";s:7:"test 25";s:5:"depth";i:1;}i:26;a:2:{s:6:"indent";s:7:"test 26";s:5:"depth";i:1;}i:27;a:2:{s:6:"indent";s:7:"test 27";s:5:"depth";i:1;}i:28;a:2:{s:6:"indent";s:7:"test 28";s:5:"depth";i:1;}i:29;a:2:{s:6:"indent";s:7:"test 29";s:5:"depth";i:1;}i:30;a:2:{s:6:"indent";s:7:"test 30";s:5:"depth";i:1;}i:31;a:2:{s:6:"indent";s:7:"test 31";s:5:"depth";i:1;}i:32;a:2:{s:6:"indent";s:7:"test 32";s:5:"depth";i:1;}i:33;a:2:{s:6:"indent";s:7:"test 33";s:5:"depth";i:1;}i:34;a:2:{s:6:"indent";s:7:"test 34";s:5:"depth";i:1;}i:35;a:2:{s:6:"indent";s:7:"test 35";s:5:"depth";i:1;}i:36;a:2:{s:6:"indent";s:7:"test 36";s:5:"depth";i:1;}i:37;a:2:{s:6:"indent";s:7:"test 37";s:5:"depth";i:1;}i:38;a:2:{s:6:"indent";s:7:"test 38";s:5:"depth";i:1;}i:39;a:2:{s:6:"indent";s:7:"test 39";s:5:"depth";i:1;}i:40;a:2:{s:6:"indent";s:7:"test 40";s:5:"depth";i:1;}i:41;a:2:{s:6:"indent";s:7:"test 41";s:5:"depth";i:1;}i:42;a:2:{s:6:"indent";s:7:"test 42";s:5:"depth";i:1;}i:43;a:2:{s:6:"indent";s:7:"test 43";s:5:"depth";i:1;}i:44;a:2:{s:6:"indent";s:7:"test 44";s:5:"depth";i:1;}i:45;a:2:{s:6:"indent";s:7:"test 45";s:5:"depth";i:1;}i:46;a:2:{s:6:"indent";s:7:"test 46";s:5:"depth";i:1;}i:47;a:2:{s:6:"indent";s:7:"test 47";s:5:"depth";i:1;}i:48;a:2:{s:6:"indent";s:7:"test 48";s:5:"depth";i:1;}i:49;a:2:{s:6:"indent";s:7:"test 49";s:5:"depth";i:1;}}