Foreach循环:重复输出错误

时间:2012-04-17 10:31:10

标签: php foreach

我的代码如下

$aNewCodes = array("93", "355", "213");
$aServiceProviderId = array();
$oTerminationRate = new TerminationRate();

foreach ($aNewCodes as $iNewCodesKey => $iNewCodesValue)
{
    $oTerminationRate->GetServiceProviders($aServiceProviderId, $iNewCodesValue);

    foreach($aServiceProviderId as $iProviderKey => $iProviderValue)
    {
        echo $iNewCodesValue." :: ".$iProviderValue."<br>";
    }
}

它给了我这样的输出 -

93 :: 1
93 :: 2
355 :: 1
355 :: 2
355 :: 1
355 :: 2
213 :: 1
213 :: 2
213 :: 1
213 :: 2
213 :: 1
213 :: 2

实际上我期待这样的输出 -

93 :: 1
93 :: 2
355 :: 1
355 :: 2
213 :: 1
213 :: 2

尝试了很多输出,但没有成功。我在哪里错过了?

1 个答案:

答案 0 :(得分:1)

您的问题是,您不会在循环的每次迭代中删除aServiceProviderId数组中的先前条目。放线

$aServiceProviderId = array();

在第一个循环内 - 就在

之前
$oTerminationRate->GetServiceProviders($aServiceProviderId, $iNewCodesValue);

你应该没事。