Jump to content

Arrays Not Working


Guest FirefoxRocks

Recommended Posts

Guest FirefoxRocks

This must be a really dumb error but I'm not sure why I can't access my arrays:

<?php	session_start();	header("Content-Type: text/html;charset=utf-8");	header("Content-Language: en-CA");	ob_start("ob_gzhandler");	date_default_timezone_set("America/Rainy_River");?><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"><html lang="en-CA"><head>	<title>Update</title></head><body>  	<div id="ac-body">		<div id="ac-content">					<h2>Update</h2>			<?php								for((int)$i=0; $i<4; ++$i) {					$team = $_POST["team"][$i];					echo $team["name"];				}			?>		</div>	</div>	</body></html>

This is what my POST array looks like:

Array( [team] => Array ( [0] => Array ( ["name"] => Team A ["points"] => 280 ) [1] => Array ( ["name"] => Team B ["points"] => 70 ) [2] => Array ( ["name"] => Team C ["points"] => 10 ) [3] => Array ( ["name"] => Team D ["points"] => 0 ) ) [player] => Array ( [0] => Array ( ["name"] => Player 1 ["team"] => Team A ["goals"] => 0 ["assists"] => 0 ) [1] => Array ( ["name"] => Player 2 ["team"] => Team C ["goals"] => 5 ["assists"] => 3 ) ))
What is the error I am making?
Link to comment
Share on other sites

Guest FirefoxRocks

Well originally it should do other stuff but I stripped it all out because I couldn't get this array thing working, right now it should just print out the names.

Link to comment
Share on other sites

Guest FirefoxRocks

Well it's not, even though the print_r output is the one I posted above. I also checked the HTTP and well obviously the page is receiving the data because I can use print_r to output it.

Link to comment
Share on other sites

Then there's something wrong with $_POST['team']. When I run this:

<?php$ar = array(  array('name' => 'aaa', 'num' => 10),  array('name' => 'bbb', 'num' => 20),  array('name' => 'ccc', 'num' => 30));print_r($ar);?>

I get this:

Array(	[0] => Array		(			[name] => aaa			[num] => 10		)	[1] => Array		(			[name] => bbb			[num] => 20		)	[2] => Array		(			[name] => ccc			[num] => 30		))

Note how the names do not have quotes around them. The names in your print_r output do have quotes, which tells me that the quotes are part of the name. e.g.:$_POST['team'][0]['"name"']

Link to comment
Share on other sites

Thank you!! That was exactly the problem, now why does $_POST have quotes around the array keys?
Most likely because you included that in the form. What does the form look like?
Link to comment
Share on other sites

Archived

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

×
×
  • Create New...