Build a tree from a flat array in PHP [duplicate]

You forgot the unset() in there bro. function buildTree(array &$elements, $parentId = 0) { $branch = array(); foreach ($elements as $element) { if ($element[‘parent_id’] == $parentId) { $children = buildTree($elements, $element[‘id’]); if ($children) { $element[‘children’] = $children; } $branch[$element[‘id’]] = $element; unset($elements[$element[‘id’]]); } } return $branch; }

.flat() is not a function, what’s wrong?

The flat method is not yet implemented in common browsers (only Chrome v69, Firefox Nightly and Opera 56). It’s an experimental feature. Therefore you cannot use it yet. You may want to have your own flat function instead: Object.defineProperty(Array.prototype, ‘flat’, { value: function(depth = 1) { return this.reduce(function (flat, toFlatten) { return flat.concat((Array.isArray(toFlatten) && (depth>1)) … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)