I have a MySQL table with a tree data structure. The fields are _id, name and parentId. When the record hasn\'t a parent, parent
_id
name
parentId
parent
function toSelect($arr, $depth = 0) { $html = ''; foreach ( $arr as $v ) { $html.= '' . str_repeat("--", $depth) . $v['name'] . '' . PHP_EOL; if ( array_key_exists('children', $v) ) { $html.= toSelect($v['children'], $depth++); } } return $html; }