3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php /* assuming top level pid = 0 */$rows = array ( array ( 'id' => 1, 'pid' => 0 ), array ( 'id' => 2, 'pid' => 1 ), array ( 'id' => 3, 'pid' => 2 ), array ( 'id' => 4, 'pid' => 0 ), array ( 'id' => 5, 'pid' => 4 ), /* ... */);$rows = array_column ( $rows, null, 'id' );foreach ( $rows as $key => $val ) { if ( $val ['pid'] ) { if ( isset ( $rows [$val ['pid']] )) { $rows [$val ['pid']]['children'][] = &$rows [$key]; } }}foreach ( $rows as $key => $val ) { if ( $val ['pid'] ) unset ( $rows [$key] );}var_dump ( $rows );
Output for 5.5.24 - 5.6.28, hhvm-3.10.0 - 3.12.0, 7.0.0 - 7.1.0
array(2) { [1]=> array(3) { ["id"]=> int(1) ["pid"]=> int(0) ["children"]=> array(1) { [0]=> array(3) { ["id"]=> int(2) ["pid"]=> int(1) ["children"]=> array(1) { [0]=> array(2) { ["id"]=> int(3) ["pid"]=> int(2) } } } } } [4]=> array(3) { ["id"]=> int(4) ["pid"]=> int(0) ["children"]=> array(1) { [0]=> array(2) { ["id"]=> int(5) ["pid"]=> int(4) } } } }
Output for 5.0.0 - 5.4.45
Fatal error: Call to undefined function array_column() in /in/vm6PF on line 2
Process exited with code 255.
Output for 4.4.5 - 4.4.9
Fatal error: Call to undefined function: array_column() in /in/vm6PF on line 2
Process exited with code 255.
Output for 4.3.2 - 4.4.4
Fatal error: Call to undefined function: array_column() in /in/vm6PF on line 2
Process exited with code 255.
Output for 4.3.0 - 4.3.1
Fatal error: Call to undefined function: array_column() in /in/vm6PF on line 2