You can use the fopen and fwrite PHP functions to do that.
It would be something like this:
<?php
// Here we create a file handler
$myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
foreach($tables as $table)
{
echo "<h2>" . $table[0] . "</h2>";
// Here, we write the value of $table[0] and a new line in the file
fwrite($myfile, "Table " . $table[0] . "\n");
$query = "DESCRIBE " . $table[0];
$result = $mysqli->query($query);
$columns = $result->fetch_all();
foreach($columns as $column)
{
echo $column[0]. '<br />';
fwrite($myfile, " - " . $column[0] . "\n");
}
}
fclose($myfile);
?>
Note: I strongly recommend you to use prepared statements for the SQL queries.