0

My code is as follows

$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>";
    }
}

And it gives me output like this -

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

Actually I am expecting output like this -

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

Tried a lot to get that output, but no success. Where am I missing out ?

2
  • You are probably missing the part where GetServiceProviders accepts its first argument by reference and adds two items to it. If it did not, your code would not give any output. Commented Apr 17, 2012 at 10:34
  • yes, it accepts by reference, and the code does gives the output.. Commented Apr 17, 2012 at 10:37

1 Answer 1

1

Your problem is that you are not deleting previous entries from the aServiceProviderId array on each iteration of the loop. Put line

$aServiceProviderId = array();

inside the first loop - right before

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

And you should be ok.

Sign up to request clarification or add additional context in comments.

8 Comments

So, resetting the value of the one variable that is not used anywhere in this snippet will fix the problem?
$oProviderForCountry = new ProviderForCountry(); is unnecessary in the code
@Jon Sorry, I copied/pasted a wrong line from the OP's code. I now corrected it. You could have just edited the answer
@Sachyn I corrected my mistake in the code - wrong copy/paste
@AleksG: Removed the downvote, but this is still wrong and two wrongs do not make a right. GetServiceProviders is in obvious need of refactoring; slapping a band-aid on a broken design does not fix the design or prevent more problems in the future.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.