rubahfgouveia Posted August 13, 2010 Share Posted August 13, 2010 Hey guys, i'm kind of stuck here, and was wondering if anyone could give me a hand. I'm kind of new in PHP, and currently I'm experiencing a cycle problem. I have a php page that recieve a list of variable number of arrays (it can be one array, or even 10), and what i want the cycle to do is, is to print out each value contained in each array. Example of variables recieved through POST: ([course1] => Array ( [0] => BBB [1] => YA ) [course2] => Array ( [0] => EWA) [course3] => Array ( [0] => aaa)) for($i = 0; $i < $num_course; $i++) // num_course is the number of arrays (course0,course1) that are recieved { $test = "course".$i; for($x = 0; $x < sizeof([color=red]$test[/color]); $x++) { echo("[color=red]$test[$x][/color]"); } } I think that the problem here is the $test variable. I don't what the sizeof($test), but yes the sizeof(course0, course1,etc) .. and also, i don't what to echo the $test[$x] variable, but yes the $course0[$x], $course1[$x], etc... i hope i made myself clear :? I would glady appreciate any help! Quote Link to comment https://forums.phpfreaks.com/topic/210609-problems-with-loops-fors-cycles/ Share on other sites More sharing options...
sasa Posted August 13, 2010 Share Posted August 13, 2010 try for($i = 0; $i < $num_course; $i++) // num_course is the number of arrays (course0,course1) that are recieved { $test = "course".$i; for($x = 0; $x < sizeof($$test); $x++) //ad one more $ { echo($$test[$x]); //same here } } Quote Link to comment https://forums.phpfreaks.com/topic/210609-problems-with-loops-fors-cycles/#findComment-1098768 Share on other sites More sharing options...
rubahfgouveia Posted August 13, 2010 Author Share Posted August 13, 2010 Hay sasa, thanks alot for the reply The code's working fine, except the last echo ( echo($$test[$x]). It's not echoing anything.. for($i = 0; $i < $num_course; $i++) // num_course is the number of arrays (course0,course1) that are recieved { $test = "course".$i; for($x = 0; $x < sizeof($$test); $x++) //ad one more $ (works fine) { echo $$teste // works out fine, it knows it's arrays echo($$test[$x]); // doesn't work. just can't get the values of the array printed out echo ($$teste[0]); //i even tried to print out a value manually, but it's not working also } } Quote Link to comment https://forums.phpfreaks.com/topic/210609-problems-with-loops-fors-cycles/#findComment-1098906 Share on other sites More sharing options...
kickstart Posted August 13, 2010 Share Posted August 13, 2010 Hi Think I would use a pair of foreach loops foreach($_POST AS $Field=>$Value) { if (substr($Field,0,6) == 'course') { foreach($Value AS $SomeArrayValue) { echo $SomeArrayValue; } } } All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/210609-problems-with-loops-fors-cycles/#findComment-1098912 Share on other sites More sharing options...
wildteen88 Posted August 13, 2010 Share Posted August 13, 2010 Wrap $test within curly braces echo(${$test}[$x]); Quote Link to comment https://forums.phpfreaks.com/topic/210609-problems-with-loops-fors-cycles/#findComment-1098914 Share on other sites More sharing options...
rubahfgouveia Posted August 13, 2010 Author Share Posted August 13, 2010 echo(${$test}[$x]); => still doesn't print out anything.. thanks, Ruben Gouveia EDIT: my fault, it's working fine !!! thanks all for the help Quote Link to comment https://forums.phpfreaks.com/topic/210609-problems-with-loops-fors-cycles/#findComment-1098919 Share on other sites More sharing options...
rubahfgouveia Posted August 13, 2010 Author Share Posted August 13, 2010 sorry to bother again: Now i'm trying to do a querie with the values from the array, but its not getting the (${$test}[$x]) values. Do i have to change the syntax again? $resultado = mysql_query("SELECT DISTINCT * FROM Curso WHERE Curso.Nome_Curso = '${$test}[$x]'"); Quote Link to comment https://forums.phpfreaks.com/topic/210609-problems-with-loops-fors-cycles/#findComment-1098995 Share on other sites More sharing options...
wildteen88 Posted August 13, 2010 Share Posted August 13, 2010 Wrap ${$test}[$x] in curly braces again, {${$test}[$x]} Quote Link to comment https://forums.phpfreaks.com/topic/210609-problems-with-loops-fors-cycles/#findComment-1099011 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.