3v4l.org

run code in 300+ PHP versions simultaneously
<?php // Fetch data from BD // $options = $stmt->query("SELECT * FROM Options")->fetch_all(PDO::FETCH_ASSOC); $options = [ ['id' => 1, 'name' => 'admin', 'ref' => 0], ['id' => 2, 'name' => 'A', 'ref' => 1], ['id' => 3, 'name' => 'B', 'ref' => 2], ['id' => 4, 'name' => 'C', 'ref' => 1], ['id' => 5, 'name' => 'D', 'ref' => 2], ['id' => 6, 'name' => 'E', 'ref' => 3], ['id' => 7, 'name' => 'F', 'ref' => 3], ['id' => 8, 'name' => 'G', 'ref' => 2], ]; $nodes = array(); $roots = array(); // init nodes indexed by IDs foreach ($options as $option) { $option['subIds'] = array(); // init subIds $nodes[$option['id']] = $option; } // build a recursive structure (by reference) foreach ($options as $option) { if ($option['ref'] == 0) { $roots[] = $option['id']; // add a root } else { $nodes[$option['ref']]['subIds'][] = $option['id']; // add a subnode } } // build recursive HTML-List function getSubtreeHTMLList($subOptionIds, $nodes) { $result = '<ul>'; foreach ($subOptionIds as $optionsId) { $result .= '<li>'; $result .= $nodes[$optionsId]['name']; if (count($nodes[$optionsId]['subIds'] > 0)) { $result .= getSubtreeHTMLList($nodes[$optionsId]['subIds'], $nodes); } $result .= '</li>'; } $result .= '</ul>'; return $result; } echo getSubtreeHTMLList($roots, $nodes); ?>
Output for 8.3.0 - 8.3.27, 8.4.1 - 8.4.14
Fatal error: Uncaught TypeError: count(): Argument #1 ($value) must be of type Countable|array, true given in /in/Q7X8F:43 Stack trace: #0 /in/Q7X8F(52): getSubtreeHTMLList(Array, Array) #1 {main} thrown in /in/Q7X8F on line 43
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/Q7X8F:43 Stack trace: #0 /in/Q7X8F(52): getSubtreeHTMLList(Array, Array) #1 {main} thrown in /in/Q7X8F on line 43
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/Q7X8F:43 Stack trace: #0 /in/Q7X8F(52): getSubtreeHTMLList(Array, Array) #1 {main} thrown in /in/Q7X8F on line 43
Process exited with code 255.
Output for 7.2.0 - 7.2.33, 7.3.16 - 7.3.33, 7.4.0 - 7.4.33
Warning: count(): Parameter must be an array or an object that implements Countable in /in/Q7X8F on line 43 Warning: count(): Parameter must be an array or an object that implements Countable in /in/Q7X8F on line 43 Warning: count(): Parameter must be an array or an object that implements Countable in /in/Q7X8F on line 43 Warning: count(): Parameter must be an array or an object that implements Countable in /in/Q7X8F on line 43 Warning: count(): Parameter must be an array or an object that implements Countable in /in/Q7X8F on line 43 Warning: count(): Parameter must be an array or an object that implements Countable in /in/Q7X8F on line 43 Warning: count(): Parameter must be an array or an object that implements Countable in /in/Q7X8F on line 43 Warning: count(): Parameter must be an array or an object that implements Countable in /in/Q7X8F on line 43 <ul><li>admin<ul><li>A<ul><li>B<ul><li>E<ul></ul></li><li>F<ul></ul></li></ul></li><li>D<ul></ul></li><li>G<ul></ul></li></ul></li><li>C<ul></ul></li></ul></li></ul>
Output for 5.6.0 - 5.6.25, 7.0.0 - 7.0.20, 7.1.0 - 7.1.20
<ul><li>admin<ul><li>A<ul><li>B<ul><li>E<ul></ul></li><li>F<ul></ul></li></ul></li><li>D<ul></ul></li><li>G<ul></ul></li></ul></li><li>C<ul></ul></li></ul></li></ul>

preferences:
123.93 ms | 413 KiB | 5 Q