Doug Wolfgram
Doug Wolfgram

Reputation: 2126

PHP arrays not evaluating

I am almost embarrassed to put this up but I am perplexed and I am on a deadline. Probably a simple typo that I can't see.

I have these three lines of code: ($p is part of a POST array)

echo "<pre>";
print_r($p);
echo "Foo:".$p['fn2'];

And the output is this.

Array
(
    ['fn1'] => uobQEC8IGQcd
    ['fn2'] => P1ZVSXBJzloE
)
Foo:

Why doesn't $p['fn2'] print? This is driving me nuts. I shortened this for the example but there are many more FNs in there too and none will display individually, yet I can see them when I dump the array. Arrrgh!

UPDATE: on suggestion, a little more code... this is inside a foreach loop that starts:

foreach ($_POST['pairs'] as $key=>$p) {

UPDATE 2:

VARDUMP:

array(10) {
    ["'fn1'"]=> string(12) "uobQEC8IGQcd"
    ["'fn2'"]=> string(12) "P1ZVSXBJzloE"
    ["'state1'"]=> string(6) "active"
    ["'state2'"]=> string(6) "active"
    ["'crit1'"]=> string(2) "27"
    ["'crit2'"]=> string(2) "24"
    ["'warn1'"]=> string(2) "18"
    ["'warn2'"]=> string(2) "12"
    ["'owner2'"]=> string(5) "Adobe"
    ["'name2'"]=> string(5) "Rack1"
}

UPDATE 3: I am setting up the post variable arrays like this (in js)

$('<input>').attr({ type: 'hidden', name: "pairs["+fn1+"]['fn1']", value:fn1 }).appendTo('form');
$('<input>').attr({ type: 'hidden', name: "pairs["+fn1+"]['fn2']", value:fn2 }).appendTo('form');

Upvotes: 2

Views: 71

Answers (3)

Gonzalo Bahamondez
Gonzalo Bahamondez

Reputation: 1371

If you write your html by this way.

<form method="post" action="test.php">
  <input name="value[test]"></input>
  <button type="submit">submit!</button>
</form>

You can access in the test.php by this way.

var_dump($_POST["value"]["test"]);

Otherwise, if you do this.

<form method="post" action="test.php">
  <input name="value['test']"></input>
  <button type="submit">submit!</button>
</form>

You will get a undefined index error if you try this.

var_dump($_POST["value"]["test"]);

Because now you should access like this.

var_dump($_POST["value"]["'test'"]);

In other words, the quotes are added automatically.

Upvotes: 0

Doug Wolfgram
Doug Wolfgram

Reputation: 2126

Aha! Vardump revealed the answer... When I was setting up the arrays from returned javascript I was doing this:

    $('<input>').attr({ type: 'hidden', name: "pairs["+fn1+"]['fn1']", value:fn1 }).appendTo('form');

instead of this:

$('<input>').attr({ type: 'hidden', name: "pairs['+fn1+'][fn1]", value:fn1 }).appendTo('form');

Shouldn't have had single quotes around array elements...

Upvotes: 0

l&#39;L&#39;l
l&#39;L&#39;l

Reputation: 47189

It's likely not working because you got the keys enclosed in two sets of quotes, which leads to an undefined index error. If you remove a pair of the quotes around the keys it should work:

Notice in your keys: ["'fn1'"]

<?php

$p = array (  "state1" => "active",
              "state2" => "active",
              "crit1" => "27",
              "crit2" => "24",
              "warn1" => "18",
              "warn2" => "12",
              "owner2" => "Adobe",
              "name2" => "Rack1" 
            );

    echo "<pre>";
    print_r($p);
    echo "Foo:".$p['state2'];

?>

Output:

<pre>Array
(
    [state1] => active
    [state2] => active
    [crit1] => 27
    [crit2] => 24
    [warn1] => 18
    [warn2] => 12
    [owner2] => Adobe
    [name2] => Rack1
)
Foo:active

Upvotes: 1

Related Questions