3v4l.org

run code in 300+ PHP versions simultaneously
<?php // result from query: SELECT * FROM depts $depts = array( array( // row #0 'deptid' => 1, 'dept_code' => '1', 'dept_name' => 'wadir Umum', 'parent_deptid' => 0, ), array( // row #1 'deptid' => 2, 'dept_code' => '101', 'dept_name' => 'bagian umum', 'parent_deptid' => 1, ), array( // row #2 'deptid' => 3, 'dept_code' => '10101', 'dept_name' => 'kepala umum', 'parent_deptid' => 2, ), array( // row #3 'deptid' => 4, 'dept_code' => '102', 'dept_name' => 'bagian privasi', 'parent_deptid' => 1, ), array( // row #4 'deptid' => 5, 'dept_code' => '1010101', 'dept_name' => 'SUb bagian Tu', 'parent_deptid' => 3, ), array( // row #5 'deptid' => 6, 'dept_code' => '1010102', 'dept_name' => 'bagian umum', 'parent_deptid' => 3, ), ); $nodes = array(); $roots = array(); // init nodes foreach ($depts as $dept) { $dept['childs'] = array(); // init childs $nodes[$dept['deptid']] = $dept; } foreach ($depts as $dept) { if ($dept['parent_deptid'] == 0) { $roots[] = $dept['deptid']; // add root } else { $nodes[$dept['parent_deptid']]['childs'][] = $dept['deptid']; // add to parents chlids list } } function getSubtreeHTMLList($deptsids, $nodes) { $result = '<ul>'; foreach ($deptsids as $deptsid) { $result .= '<li>'; $result .= $nodes[$deptsid]['dept_name']; if (count($nodes[$deptsid]['childs'] > 0)) { $result .= getSubtreeHTMLList($nodes[$deptsid]['childs'], $nodes); } $result .= '</li>'; } $result .= '</ul>'; return $result; } echo getSubtreeHTMLList($roots, $nodes); ?>
Output for 8.3.0 - 8.3.25, 8.4.1 - 8.4.12
Fatal error: Uncaught TypeError: count(): Argument #1 ($value) must be of type Countable|array, true given in /in/mnV5m:67 Stack trace: #0 /in/mnV5m(76): getSubtreeHTMLList(Array, Array) #1 {main} thrown in /in/mnV5m on line 67
Process exited with code 255.
Output for 8.0.10 - 8.0.30, 8.1.0 - 8.1.33, 8.2.0 - 8.2.29
Fatal error: Uncaught TypeError: count(): Argument #1 ($value) must be of type Countable|array, bool given in /in/mnV5m:67 Stack trace: #0 /in/mnV5m(76): getSubtreeHTMLList(Array, Array) #1 {main} thrown in /in/mnV5m on line 67
Process exited with code 255.
Output for 8.0.0 - 8.0.9
Fatal error: Uncaught TypeError: count(): Argument #1 ($var) must be of type Countable|array, bool given in /in/mnV5m:67 Stack trace: #0 /in/mnV5m(76): getSubtreeHTMLList(Array, Array) #1 {main} thrown in /in/mnV5m on line 67
Process exited with code 255.
Output for 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33
Warning: count(): Parameter must be an array or an object that implements Countable in /in/mnV5m on line 67 Warning: count(): Parameter must be an array or an object that implements Countable in /in/mnV5m on line 67 Warning: count(): Parameter must be an array or an object that implements Countable in /in/mnV5m on line 67 Warning: count(): Parameter must be an array or an object that implements Countable in /in/mnV5m on line 67 Warning: count(): Parameter must be an array or an object that implements Countable in /in/mnV5m on line 67 Warning: count(): Parameter must be an array or an object that implements Countable in /in/mnV5m on line 67 <ul><li>wadir Umum<ul><li>bagian umum<ul><li>kepala umum<ul><li>SUb bagian Tu<ul></ul></li><li>bagian umum<ul></ul></li></ul></li></ul></li><li>bagian privasi<ul></ul></li></ul></li></ul>
Output for 5.5.0 - 5.5.38, 5.6.0 - 5.6.40, 7.0.0 - 7.0.33, 7.1.0 - 7.1.27
<ul><li>wadir Umum<ul><li>bagian umum<ul><li>kepala umum<ul><li>SUb bagian Tu<ul></ul></li><li>bagian umum<ul></ul></li></ul></li></ul></li><li>bagian privasi<ul></ul></li></ul></li></ul>

preferences:
111.32 ms | 413 KiB | 5 Q