Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 每个()都已弃用,难以摆脱_Php_Foreach_Each_Php 7.2 - Fatal编程技术网

Php 每个()都已弃用,难以摆脱

Php 每个()都已弃用,难以摆脱,php,foreach,each,php-7.2,Php,Foreach,Each,Php 7.2,我正在使用sql数据库备份脚本,代码的一部分是: if (!isset($table_select)) { $result = $dbc->prepare("show tables"); $i=0; $table=""; $tables = $dbc->executeGetRows($result); foreach ($tables as $table_array) { list(,$table) = each($table_array); $exclude_this_table

我正在使用sql数据库备份脚本,代码的一部分是:

if (!isset($table_select))
{
$result = $dbc->prepare("show tables");
$i=0;
$table="";
$tables = $dbc->executeGetRows($result);
foreach ($tables as $table_array)
{
list(,$table) = each($table_array);
$exclude_this_table = isset($table_exclude)? in_array($table,$table_exclude) : false;
if(!$exclude_this_table) $table_select[$i]=$table;
$i++;
}
}
我不知道如何摆脱这部分:
list(,$table)=each($table\u数组)

由于
each()
已被弃用,有人知道解决方案吗?

非常感谢你

您不希望在php脚本中执行此操作。我强烈建议您尽可能使用cronjob来完成此任务

您可以使用

crontab -e
并添加如下内容:

0 1 * * * mysqldump -e --user=username --password='password' dbname | gzip | uuencode
sql_backup.gz | mail example@example.com
上述示例将在每天凌晨1点启动作业

你可以在这里结账


希望有帮助!:)

由于您没有使用
每个
提供的键,因此在此上下文中不需要它

例子: 由于数组是通过副本而不是引用传递的,如果在同一上下文中未再次使用
$table\u array
,则可以使用
array\u shift()
作为替代方法

foreach ($tables as $table_array) {
    $table = array_shift($table_array);
    var_dump($table);
}
请记住,array_shift检索当前值,并将其从数组中删除

由于
each()
检索当前键值对并将指针前进到下一个值,因此可以将其替换为
current()
next()

如果在使用
current
next
时确实需要当前值的
键,则可以使用
key()


由于您正在使用的自动备份脚本利用了,您可以通过使用
fetchAll(PDO::FETCH_列)来降低复杂性
而不是
executeGetRows
,后者将检索0索引列的平面数组,而不是列键值对

$result = $dbc->prepare("show tables");
$result->execute();
$i=0;
$table="";
$tables = $result->fetchAll(\PDO::FETCH_COLUMN);
foreach ($tables as $table) {
     //...
}

你为什么使用php?而不是简单地导出它?这是因为这个脚本可以定期将数据库发送到我的邮件(并将其保存在云存储上)。1行
mysqldump--add drop table--user=“*”--password=“*”--host=“127.0.0.1”dbname | mail-s备份user@example.com
非常感谢您。你是我的超级英雄。工作起来很有魅力。非常感谢您的好意。@Aska如果您认为我的答案解决了您的问题,请投票表决或将其标记为已接受的答案,以便其他人知道您的问题不再需要答案。
foreach ($tables as $table_array) {
    $key = key($table_array);
    $table = current($table_array);
    var_dump($key, $table);
    next($table_array);
}
$result = $dbc->prepare("show tables");
$result->execute();
$i=0;
$table="";
$tables = $result->fetchAll(\PDO::FETCH_COLUMN);
foreach ($tables as $table) {
     //...
}