Problem with multi-dimensional array inside a For loop
Posted: Sat Sep 15, 2007 8:58 pm
scottayy | Please use
Thank you to anyone who can shed some light!
Sincerely,
David
scottayy | Please use
Code: Select all
,Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
I've struggled with this for the past 2 hours, and I could really use a second pair of eyes. This has to be about the simplest thing, and yet I can't get it to work.
I create and fill a multi-dimensional array called $form_fields inside a FOR loop. When I exit the FOR loop, I cannot print the values of the array. I can, however, print the values while inside of the FOR loop. I don't recall there being any variable scope issues relating to nested loops, so I am completely at a loss here. I make no function calls in my code, so that's not an issue. As for the rest of the code, I've created a simple form with 10 rows, with 3 fields per row, gather video ID, song title, and artist name data from the user. When the user submits the data for the form (POST method), I reload the data into the form, and it works just fine. But below the form, I want to create a special preview for the user, with the data displayed in particular format. No big deal. But I can't get a single value from the previously loaded array to print to the screen (outside of the FOR loop in which it is created). Help! Here is the FOR loop and I show you where I try to print both inside and outside of the FOR loop:Code: Select all
<?php
for($counter = 1; $counter <= 10; $counter++)
{
// ASSIGN FORM ROW VALUES TO VARIABLES:
$vidID = "txtVidID_".$counter;
$artistName = "txtArtist_".$counter;
$songTitle = "txtSong_".$counter;
$vidID=$_POST[$vidID];
$artistName=$_POST[$artistName];
$songTitle = $_POST[$songTitle];
// POPULATE ARRAY WITH FORM VALUES:
$form_fields = array($counter=>array(vidID=>$vidID, artistName=>$artistName, songTitle=>$songTitle));
// THE FOLLOWING LINE PRINTS JUST FINE -- AS IT IS INSIDE THE FOR LOOP:
//print($form_fields[$counter][artistName]);
}
// THE FOLLOWING LINE WILL NOT PRINT AND I HAVE NO IDEA WHY:
print($form_fields[1][artistName]);
?>Sincerely,
David
scottayy | Please use
Code: Select all
,Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]