Converting for loop to foreach

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • micky125
    New Member
    • Sep 2008
    • 36

    #1

    Converting for loop to foreach

    Hey guys i posted up to a reply some other detail but the main point of it is to know how if you use foreach ... can u access different arrays ... take

    Code:
     for ($i = 1; $i <= count($Hours); $i++) //This is compensation for actual work
            {
            $actualValues[$i] = $Hours[$i] * $Wage[$i];
            $theValue += $actualValues[$i];
        }
    if you look at http://bytes.com/forum/thread844597.html you will see how i have tried doing it and the reasons behind it...
    Last edited by pbmods; Oct 13 '08, 07:36 PM. Reason: Fixed link.
  • pbmods
    Recognized Expert Expert
    • Apr 2007
    • 5821

    #2
    Heya, Mickey.

    Here's what you're looking for:

    [code=php]
    foreach( $Hours as $i => $loggedHours )
    {
    $actualValues[$i] = $loggedHours * $Wage[$i];
    $theValue += $actualValues[$i];
    }
    [/code]

    Comment

    • micky125
      New Member
      • Sep 2008
      • 36

      #3
      lol its that easy... i actually had something v similar to that but thought it wudnt work cus Wage[$i] wasnt part the for loop...

      Well was working away at me program and the only errors i have i think come from the for loops so hopefully when I do this i can be finished with it... then i'll start using it for small programs within work =) This should be fun lol.

      Cheers again PBMods!

      Comment

      Working...