mo Posted January 26, 2009 Share Posted January 26, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/142487-why-does-this-nested-foreach-not-work/ Share on other sites More sharing options...
premiso Posted January 26, 2009 Share Posted January 26, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/142487-why-does-this-nested-foreach-not-work/#findComment-746571 Share on other sites More sharing options...
rhodesa Posted January 26, 2009 Share Posted January 26, 2009 if premiso's code doesn't fix it, post the output of: print_r($_POST['OptionGroups']); Quote Link to comment https://forums.phpfreaks.com/topic/142487-why-does-this-nested-foreach-not-work/#findComment-746577 Share on other sites More sharing options...
mo Posted January 26, 2009 Author Share Posted January 26, 2009 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> Quote Link to comment https://forums.phpfreaks.com/topic/142487-why-does-this-nested-foreach-not-work/#findComment-746628 Share on other sites More sharing options...
rhodesa Posted January 26, 2009 Share Posted January 26, 2009 are you looking for this maybe: foreach($_POST['OptionGroups'] as $Gval => $Gvalue) { foreach($_POST[$Gvalue] as $val => $value) { } } Quote Link to comment https://forums.phpfreaks.com/topic/142487-why-does-this-nested-foreach-not-work/#findComment-746631 Share on other sites More sharing options...
rhodesa Posted January 26, 2009 Share Posted January 26, 2009 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) { } } Quote Link to comment https://forums.phpfreaks.com/topic/142487-why-does-this-nested-foreach-not-work/#findComment-746634 Share on other sites More sharing options...
mo Posted January 26, 2009 Author Share Posted January 26, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/142487-why-does-this-nested-foreach-not-work/#findComment-746680 Share on other sites More sharing options...
premiso Posted January 26, 2009 Share Posted January 26, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/142487-why-does-this-nested-foreach-not-work/#findComment-746703 Share on other sites More sharing options...
rhodesa Posted January 26, 2009 Share Posted January 26, 2009 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> Quote Link to comment https://forums.phpfreaks.com/topic/142487-why-does-this-nested-foreach-not-work/#findComment-746710 Share on other sites More sharing options...
mo Posted January 26, 2009 Author Share Posted January 26, 2009 When I remove the [] for the name of the radio buttons, I receive error "Warning: Invalid argument supplied for foreach() in". Quote Link to comment https://forums.phpfreaks.com/topic/142487-why-does-this-nested-foreach-not-work/#findComment-746729 Share on other sites More sharing options...
rhodesa Posted January 26, 2009 Share Posted January 26, 2009 um...can you post the latest copy of the code...cus it should work Quote Link to comment https://forums.phpfreaks.com/topic/142487-why-does-this-nested-foreach-not-work/#findComment-746730 Share on other sites More sharing options...
mo Posted January 26, 2009 Author Share Posted January 26, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/142487-why-does-this-nested-foreach-not-work/#findComment-746787 Share on other sites More sharing options...
rhodesa Posted January 26, 2009 Share Posted January 26, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/142487-why-does-this-nested-foreach-not-work/#findComment-746789 Share on other sites More sharing options...
mo Posted January 26, 2009 Author Share Posted January 26, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/142487-why-does-this-nested-foreach-not-work/#findComment-746792 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.