<?php
// Получаем таблицы asterisk_phone_table
$phone_tables = [];
$sqlQuery = 'SHOW TABLES LIKE "' . $config['table_prefix'] . 'module_asterisk_phone_table%"';
$result_asterisk_phone = run_query($sqlQuery);
while ($table = sql_fetch_array($result_asterisk_phone)) {
    $phone_tables[] = $table[0];
}

foreach ($phone_tables as $phone_table) {
    $table_id = (int)preg_replace('/[^0-9]/', '', $phone_table);
    if ($table_id > 0) {
        $result_check_table = run_query('SELECT id FROM ' . $config['table_prefix'] . 'tables WHERE id = ' . $table_id);
        if (!($row = sql_fetch_assoc($result_check_table))) {
            run_query('DROP TABLE ' . $phone_table);
        }
    }
}

