2012-04-17 96 views
0

我的代码如下的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 

尝试了很多以获得该输出,但没有成功。我错过了什么地方?

+0

您可能错过了“GetServiceProviders'通过引用*接受其第一个参数*的部分,并向其中添加了两个项目。如果没有,你的代码不会给任何*输出。 – Jon 2012-04-17 10:34:54

+0

是的,它通过引用接受,并且代码确实给出了输出结果。 – skos 2012-04-17 10:37:21

回答

1

你的问题是你没有删除在循环的每次迭代中从aServiceProviderId数组中删除以前的条目。放线

$aServiceProviderId = array(); 

第一循环中 - 之前

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

的权利,你应该没问题。

+0

因此,重置**代码中未使用**的一个变量的值将解决此问题? – Jon 2012-04-17 10:39:28

+0

'$ oProviderForCountry = new ProviderForCountry();'代码中不需要' – skos 2012-04-17 10:40:17

+0

@Jon对不起,我从OP的代码中复制/粘贴了一行错误。我现在纠正它。你可以刚刚编辑答案 – 2012-04-17 10:40:30