如何在不使用不推荐的“每个”功能的情况下重写此php“ if”语句?

问题描述 投票:0回答:1

我如何重写这段php脚本,以使其不再在'if'语句中使用不推荐使用的函数'each'?

if (list($_basic_auth_realm, $_basic_auth_header) = each($_auth_creds))
{
     ...
}

在很多方面为您输入!

V。

[编辑]。此if语句不在循环中。它是一个更大的块的一部分:

if (!empty($_basic_auth_header))
    {
    ....
    }
    else if (!empty($_basic_auth_realm) && isset($_auth_creds[$_basic_auth_realm]))
    {
    ....
    }
    else if (list($_basic_auth_realm, $_basic_auth_header) = each($_auth_creds))
    {
    ....
    }

$$ basic_auth_realm,$ _basic_auth_header是字符串$ _auth_creds是一个数组

我不太了解这个'if'语句是如何工作的。我只尝试更新执行时返回警告的脚本。如Abdullah Arif所写,它被用作我NAS上的php代理:https://github.com/emersion/phproxy

php each
1个回答
1
投票

使用您自己的变量来跟踪数组中的当前索引,而不是依赖于数组的内部状态。

$auth_creds_index = 0;
...
    else if (list($_basic_auth_realm, $_basic_auth_header) = $_auth_creds[$auth_creds_index++])

[当前使用each($_auth_creds)的每个位置都应使用$_auth_creds[$auth_creds_index++],它们将获得数组的连续元素。

如果使用新数组重新分配变量,则需要将变量重置为0

您还可以为自动定义所有这些的数组定义一个类包装器。

© www.soinside.com 2019 - 2024. All rights reserved.