Author Topic: Correct Format for - foreach + two statments  (Read 490 times)

  • Offline neXus

  • Posts: 8,749
  • Hero Member
Correct Format for - foreach + two statments
on: June 06, 2007, 18:41:03 PM
Just to recap that a foreach loop to access array data is..

Code: [Select]

foreach ($_POST[checkbox] as $bgvalue) {
//Do Stuff
}


Which is fine, but say you have some data from a form that has two elements of multiple data that need to be used - basically two arrays

(In my case its a lot of choices with two data elements of each choice)

I thought the form for it was...

Code: [Select]

foreach ($_POST[checkbox] as $bgvalue;  $_POST[hiddenlink] as $lkvalue) {
//do stuff
}


Which it is not as you get the error..
Parse error: parse error, unexpected ;, expecting )

Anyone know the correct format for that please?

Thanks

  • Offline cornet

  • Posts: 143
  • Full Member
Re:Correct Format for - foreach + two statments
Reply #1 on: June 07, 2007, 00:03:59 AM
Not sure where you got that idea from... you want nested foreach loops...

Code: [Select]

foreach ($_POST[checkbox] as $bgvalue) {
   foreach ($_POST[hiddenlink] as $lkvalue) {

       //do stuff

    }
}



Cornet

  • Offline neXus

  • Posts: 8,749
  • Hero Member
Re:Correct Format for - foreach + two statments
Reply #2 on: June 07, 2007, 02:06:20 AM
Quote from: cornet
Not sure where you got that idea from... you want nested foreach loops...

Code: [Select]

foreach ($_POST[checkbox] as $bgvalue) {
   foreach ($_POST[hiddenlink] as $lkvalue) {

       //do stuff

    }
}



Cornet


Nope, becuase that returns 4 lots of 1 item each time when it should only be the once

0 Members and 1 Guest are viewing this topic.