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); ?>

This is an error 500

Value for `_results` contains invalid data `array`


preferences:
216.18 ms | 2233 KiB | 9 Q