views:

111

answers:

5

I'm having an issue getting the value of a nested array element. Here's what I've got:

print_r($environment);

// Outputs
Array
(
    [0] => Array
        (
            ['parameter'] => Vibration
            ['conditions'] => 204
            ['method'] => D
        )

    [1] => Array
        (
            ['parameter'] => Immersion
            ['conditions'] => 104
            ['method'] => B
        )

    [2] => Array
        (
            ['parameter'] => Shock
            ['conditions'] => 213
            ['method'] => I
        )

    [3] => Array
        (
            ['parameter'] => Humidity
            ['conditions'] => 106
            ['method'] => -
        )
)


print_r($environment[0]);

// Outputs
Array
(
    ['parameter'] => Vibration
    ['conditions'] => 204
    ['method'] => D
)


echo $environment[0]['parameter'];

// Nothing

Maybe I've been looking at this for too long. Any ideas would be greatly appreciated.

EDIT: reporting is set to E_ALL and only reports this (which is the line that contains the echo statement above):

Notice: Undefined index: parameter in /home/nicolasc/public_html/3rdparty/ecicaps/admin/products-manage.php on line 132

var_dump($environment[0]['parameter']); outputs NULL

var_dump($environment, $environment[0], $environment[0]['parameter']);

// Outputs
array(12) {
  [0]=>
  array(3) {
    ["'parameter'"]=>
    string(9) "Vibration"
    ["'conditions'"]=>
    string(3) "204"
    ["'method'"]=>
    string(1) "D"
  }
  [1]=>
  array(3) {
    ["'parameter'"]=>
    string(9) "Immersion"
    ["'conditions'"]=>
    string(3) "104"
    ["'method'"]=>
    string(1) "B"
  }
  [2]=>
  array(3) {
    ["'parameter'"]=>
    string(5) "Shock"
    ["'conditions'"]=>
    string(3) "213"
    ["'method'"]=>
    string(1) "I"
  }
  [3]=>
  array(3) {
    ["'parameter'"]=>
    string(8) "Humidity"
    ["'conditions'"]=>
    string(3) "106"
    ["'method'"]=>
    string(1) "-"
  }
  [4]=>
  array(3) {
    ["'parameter'"]=>
    string(13) "Thermal Shock"
    ["'conditions'"]=>
    string(3) "107"
    ["'method'"]=>
    string(1) "A"
  }
  [5]=>
  array(3) {
    ["'parameter'"]=>
    string(4) "Life"
    ["'conditions'"]=>
    string(3) "108"
    ["'method'"]=>
    string(1) "F"
  }
  [6]=>
  array(3) {
    ["'parameter'"]=>
    string(0) ""
    ["'conditions'"]=>
    string(0) ""
    ["'method'"]=>
    string(0) ""
  }
  [7]=>
  array(3) {
    ["'parameter'"]=>
    string(0) ""
    ["'conditions'"]=>
    string(0) ""
    ["'method'"]=>
    string(0) ""
  }
  [8]=>
  array(3) {
    ["'parameter'"]=>
    string(0) ""
    ["'conditions'"]=>
    string(0) ""
    ["'method'"]=>
    string(0) ""
  }
  [9]=>
  array(3) {
    ["'parameter'"]=>
    string(0) ""
    ["'conditions'"]=>
    string(0) ""
    ["'method'"]=>
    string(0) ""
  }
  [10]=>
  array(3) {
    ["'parameter'"]=>
    string(0) ""
    ["'conditions'"]=>
    string(0) ""
    ["'method'"]=>
    string(0) ""
  }
  [11]=>
  array(3) {
    ["'parameter'"]=>
    string(0) ""
    ["'conditions'"]=>
    string(0) ""
    ["'method'"]=>
    string(0) ""
  }
}
array(3) {
  ["'parameter'"]=>
  string(9) "Vibration"
  ["'conditions'"]=>
  string(3) "204"
  ["'method'"]=>
  string(1) "D"
}
NULL

$environment is populated by form fields ($environment = $_POST['Environment'])

A: 

You can access them like this:

echo $environment[0]['parameter'];
Justin Ethier
A: 

parameter is misspelled probably.

Col. Shrapnel
A: 

It could be that $environment is not a plain array, but an instance of the Iterator class which is broken.

Sjoerd
$environment is an array of values submitted from a form. Now if it's possible to inadvertently use an iterator class I'm all ears for how to figure out where that might happen so I can kill it. ;)
John Conde
+2  A: 

The array keys have quotes. Try

$env[0]["'parameter'"]

so that the single quotes are part of the array key.

Sjoerd
That was it. I had single quotes surrounding the field names which where then part of the array key upon submission.
John Conde
A: 

it looks to me you have wrong data in key calues: instead of ['parameter'] you have ["'parameter'"] for that you need to access is like echo $environment[0][["'parameter'"];

Melkior