views:

270

answers:

3

I have an array ($form) which retreives some information from $_POST:

$form = $_POST['game'];

Now I want to work with the values in this array, but I somehow fail.

For debugging I used these commands (in the exact same order, with no extra lines inbetween):

print_r($form);
echo '#' . $form['System_ID'] . "#";

and as returned output I get:

Array
(
    ['Title'] => Empire: Total War - Special Forces
    ['Genre_ID'] => 1
    ['Type'] => Spiel
    ['System_ID'] => 1
)
##

Any ideas where my System_ID went? It's there in print_r, but not in the next line for echo?!?

A: 

It works for me:

<?
  $form['System_ID'] = 1;
  print_r($form);
  echo '#' . $form['System_ID'] . '#';
?>

Output:

% php foo.php
Array
(
    [System_ID] => 1
)
#1#

PHP 5.2.6, on Fedora Core 10

EDIT - note that there's a hint to the real cause here. In my code the print_r output (correctly) shows the array keys without single quotes around them. The original poster's keys did have quotes around them in the print_r output, showing that somehow the actual key contained the quote marks.

Alnitak
+7  A: 

Alright, I found the solution myself (a.k.a. d'oh!)

I added another

var_dump($form);

for further analysis and this is what I got:

array(4) {
  ["'Title'"]=>
  string(34) "Empire: Total War - Special Forces"
  ["'Genre_ID'"]=>
  string(1) "1"
  ["'Type'"]=>
  string(5) "Spiel"
  ["'System_ID'"]=>
  string(1) "1"
}

Notice the single quote inside the double quote?

Looks as if you're not allowed to use the single quote in html forms or they will be included in the array key:

Wrong: <input type="text" name="game['Title']" />
Correct: <input type="text" name="game[Title]" />
BlaM
Ah, I wondered why your print_r output had quotes around the keys and mine didn't... :)
Alnitak
+3  A: 

print_r() doesn't put quotes around keys - for debugging i'd recommend ditching print_r altogether. var_export or var_dump are better.

even better: use firephp. it sends the debug info via headers, so it doesn't mess up your output and thus is even usable with ajax. output is displayed nicely with firebug including syntax coloring for data structures.

and it's even easier to use: just fb($myvar);

Schnalle
+1 for Firephp - I didn't know about that!
Alnitak
Yapp, FirePHP is great. I'm just too lazy to include it most of the time.
BlaM