Jump to content

Why does this nested foreach not work


mo

Recommended Posts

I have 2 arrays. The first is the key group and the second is the subgroup.

 

I am basically trying to loop at key group than at sub-group where sub-group name (array name) = current key group.

 

foreach($_POST['OptionGroups'] as $Gval => $Gvalue) {

 

  foreach($Gvalue as $val => $value) {

 

  }

}

 

$Gvalue contains the name of my second arry but I get error "Warning: Invalid argument supplied for foreach() in ..." on the second foreach loop.

Link to comment
https://forums.phpfreaks.com/topic/142487-why-does-this-nested-foreach-not-work/
Share on other sites

foreach($_POST['OptionGroups'] as $Gval => $Gvalue) {

  if (is_array($Gvalue)) {
       foreach($Gvalue as $val => $value) {
             
      }
  }
}

 

Given that post data can be somewhat un-reliable. I would add an is_array check. I would also make sure that your form posts a multi-dimm array. My bet is it does not.

Array OptionGroups prints fine but it's the second foreach. If I specify the name of the second foreach's array explicitly, it works. However I want the second foreach to be dynamic.

 

Array definitions:

Main Group

<input type=\"hidden\" name=\"OptionGroups[]\" value=\"".$ArrayRow['option_type']."\">

 

Sub Group

<input type=\"radio\" name=\"".$OptGroupName."[]\" value=\"$OptAttrArray\"></td>

or get rid of the hidden input and use a two level array:

<input type=\"radio\" name=\"OptionGroups[".$OptGroupName."][]\" value=\"$OptAttrArray\"></td>

 

then you can use your original PHP:

foreach($_POST['OptionGroups'] as $Gval => $Gvalue) {
  foreach($Gvalue as $val => $value) {

  }
}

The below worked. Don't know why I didn't think of it.

 

foreach($_POST['OptionGroups'] as $Gval => $Gvalue) {

  foreach($_POST[$Gvalue] as $val => $value) {

           

  }

}

 

Now when I echo $value, I get the word Array printed to the screen and not the value and if I use $value[0][0] I get an error.

The below worked. Don't know why I didn't think of it.

 

foreach($_POST['OptionGroups'] as $Gval => $Gvalue) {

   foreach($_POST[$Gvalue] as $val => $value) {

            

   }

}

 

Now when I echo $value, I get the word Array printed to the screen and not the value and if I use $value[0][0] I get an error.

 

Do a print_r on that value and see what the index should be.

looking at it...the value won't ever be an array since they are radio buttons, so change this:

<input type=\"radio\" name=\"".$OptGroupName."[]\" value=\"$OptAttrArray\"></td>

to

<input type=\"radio\" name=\"".$OptGroupName."\" value=\"$OptAttrArray\"></td>

while($ArrayRow = mysql_fetch_array($sqlStr)) {

 

......

 

//Build array of option attributes

$OptAttrArray = array( array($strOptId,$strPrice,$strIsOption,$strBtype,$strMaxOptions) );

 

 

<input type=\"hidden\" name=\"OptionGroups[]\" value=\"".$ArrayRow['option_type']."\">

 

echo "<tr><td class=\"dataListItem\"><input type=\"radio\" name=\"".$OptGroupName."\" value=\"$OptAttrArray\"></td><tr>";

 

 

foreach($_POST['OptionGroups'] as $Gval => $Gvalue) {

  foreach($_POST[$Gvalue] as $val => $value) {

 

  }

}

 

 

}

Error = Warning: Invalid argument supplied for foreach() in....line 66.

the code doesn't really make sense

 

-$OptAttrArray is supposed to be an array of what?

-is there more then one radio button for each option group?

 

...forget the foreach($_POST) stuff for now...the part that builds the form needs some work first

rhodesa,

 

I thought about that after I posted my last reply. I was trying to build and array with various columns w/o building a multidimensional array.

 

I just changed my code to have $OptAttrArray just contain a string of concatenated values and I will just parse out the values later during form processing.

 

Thanks. This is resolved for now.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.