Guest FirefoxRocks Posted September 15, 2009 Share Posted September 15, 2009 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 More sharing options...
jeffman Posted September 15, 2009 Share Posted September 15, 2009 When the value of an array member is a string, it should be quoted. So, just looking at the first instance:"name" => Team AShould be"name" => 'Team A'That got your array working for me, anyway. Link to comment Share on other sites More sharing options...
Guest FirefoxRocks Posted September 15, 2009 Share Posted September 15, 2009 It's a $_POST array and what I posted was the print_r report. Link to comment Share on other sites More sharing options...
justsomeguy Posted September 15, 2009 Share Posted September 15, 2009 So what happens on the page? And why in the heck are you casting 0 as an integer? Is there an issue with the gzipped output? Link to comment Share on other sites More sharing options...
Guest FirefoxRocks Posted September 15, 2009 Share Posted September 15, 2009 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 More sharing options...
justsomeguy Posted September 15, 2009 Share Posted September 15, 2009 Right, if that's what you have in post then that loop should print the names out. Link to comment Share on other sites More sharing options...
Guest FirefoxRocks Posted September 15, 2009 Share Posted September 15, 2009 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 More sharing options...
justsomeguy Posted September 15, 2009 Share Posted September 15, 2009 Do either of these work: foreach ($_POST['team'] as $team){ echo $team['name'];} for ($i = 0; $i < count($_POST['team']); $i++){ echo $_POST['team'][$i]['name'];} Link to comment Share on other sites More sharing options...
Guest FirefoxRocks Posted September 16, 2009 Share Posted September 16, 2009 Nope, originally I was using foreach because I had an object before, but I tried while and for and they still do not echo anything. Link to comment Share on other sites More sharing options...
justsomeguy Posted September 16, 2009 Share Posted September 16, 2009 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 More sharing options...
Guest FirefoxRocks Posted September 17, 2009 Share Posted September 17, 2009 Thank you!! That was exactly the problem, now why does $_POST have quotes around the array keys? Link to comment Share on other sites More sharing options...
Ingolme Posted September 17, 2009 Share Posted September 17, 2009 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 More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.