function sortCats($result, $p_id) { $structured = array(); if (is_array($result)) { foreach ($result as $row) { if ($row['p_id'] == $p_id) { $id = $row['id']; $structured[$id]['row'] = $row; $structured[$id]['children'] = FormDB::sortCats($result, $id); } } } return $structured; }