Problem with multi-dimensional array inside a For loop

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dangcookie
Forum Newbie
Posts: 1
Joined: Sat Sep 15, 2007 8:41 pm

Problem with multi-dimensional array inside a For loop

Post by dangcookie »

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]);
	
?>
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]
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

$form_fields = array($counter=>array(vidID=>$vidID, artistName=>$artistName, songTitle=>$songTitle));
This does not append a new element to the array $form_fields but assigns a new array with one one element $counter=>xyz.

try

Code: Select all

$form_fields = array();
for($counter = 1; $counter <= 10; $counter++)
{
  $vidID = isset($_POST["txtVidID_".$counter]) ? $_POST["txtVidID_".$counter]: '?';
  $artistName = isset($_POST["txtArtist_".$counter]) ? $_POST["txtArtist_".$counter]: '?';
  $songTitle = isset($_POST["txtSong_".$counter]) ? $_POST["txtSong_".$counter] : '?';

  $form_fields[$counter] = array('vidID'=>$vidID, 'artistName'=>$artistName, 'songTitle'=>$songTitle);
}

echo $form_fields[1]['artistName'], " ";
print($form_fields);
instead.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Your array keys are also in need of quotes. :P

'key' => $value (notice the single quotes)
and $array['value'] (again the single quotes)

This will throw out notices all over the place and fill up your error log pretty quickly. ;)
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Hemlata
Forum Commoner
Posts: 35
Joined: Mon Sep 10, 2007 5:40 am
Location: India
Contact:

Post by Hemlata »

feyd | 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]


Hello [b]dangcookie,[/b]

In your script, when you print the array [b]within for loop[/b], it[b] prints [/b]the data, but when you print it [b]outside the loop[/b], it [b]doesnt[/b]. The reason is [b]wrong format for array calling..[/b]

If you print the complete array [b]within loop[/b], you will be getting result as..

Code: Select all

Array
(
    [1] => Array
        (
            [vidID] => 1
            [artistName] => test
            [songTitle] => test title
        )
)
and if you are printing this complete array outside, the result will be..

Code: Select all

Array
(
    [10] => Array
        (
            [vidID] => 
            [artistName] => test
            [songTitle] => 
        )
)
means the array for just last counter value... This is because you are crating new array all the time with you code..

Code: Select all

// POPULATE ARRAY WITH FORM VALUES:
	$form_fields = array
	(
		$counter=>array
		(
			'vidID'=>$vidID
			, 'artistName'=>$artistName
			, 'songTitle'=>$songTitle
		)
	);
The solution could be to create the array in the following manner so that, now you can get the result for print($form_fields[1][artistName]); .

Code: Select all

$form_fields[$counter] = array
	(
		'vidID'=>$vidID
		, 'artistName'=>$artistName
		, 'songTitle'=>$songTitle
	);
Regards,


feyd | 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]
Post Reply