-2

$input is an array with some values, and I am sure the key from the bellow code example exists. $points is an array of strings.

I am simply getting Undefined index: (with no other information) for this code:

foreach ($points as $point) {
     $point_value = $input[$point];
...

but if I dump it:

foreach ($points as $point) {
     die(var_dump($input[$point]));
...

then I get the value correctly, without having Undefined index reported.

Now, I am using

$point_value = isset($input[ $point ]) ? $input[ $point ] : '';

and it works fine. But I was wondering why does that happen? Why is the index not being initialized in the first case, but it is being initialized when I simply dump the value?

I read around about this "problem", but could not really grasp the idea behind it. Could I get some simpler explanation, please?

halfer
  • 19,824
  • 17
  • 99
  • 186
Milkncookiez
  • 6,817
  • 10
  • 57
  • 96
  • Please show us the output of: `var_dump($points);` and `var_dump($input);` – Rizier123 Mar 26 '15 at 18:14
  • `$input` is quite big, there is no point in showing it here, since I stated that I am sure it has a key that is the same string as the value of `$point`. Furthermore, `var_dump($input[$point])` shows value. I gave enough information in the post... – Milkncookiez Mar 26 '15 at 18:23
  • Show us the first 5 elements of both arrays! From the var_dump output, from the source code! Also please post the exact error message you get – Rizier123 Mar 26 '15 at 18:24
  • `Why is the index not being initialized when I use it for assignment` - where are you using it for assignment? – James Mar 26 '15 at 18:53
  • @James, sorry, just wrong way of expressing myself. I corrected it. :) – Milkncookiez Mar 26 '15 at 18:55
  • The notice you are seeing means that the index you are attempting to access does not exist. I'm not sure, but I wonder if `var_dump()`, being essentially for debugging, is more forgiving than an ordinary access. The `isset()` approach is a good way to solve this, if you might be accessing indices that do not exist. – halfer Mar 26 '15 at 19:29

2 Answers2

1
$points = ["n", "s", "e", "w"];
//$points = ["n", "s", "e", "w", "doesnotexist"];
$input = array("n"=>"north", "s"=>"south", "e"=>"east", "w"=>"west");

foreach ($points as $point) {
     $point_value = $input[$point];
     echo $point_value;
}

This code works fine.

Uncomment line 2 - the code breaks as yours does.

Therefore, in your code, one of the strings contained in the $points array does not have a corresponding key in the $input array.

James
  • 20,957
  • 5
  • 26
  • 41
0

then I get the value correctly, without having Undefined index thrown in my face.

No, you don't. You misinterpret the results.. and you falsely represent them here.

The logical explanation is that in the loop for the first value the index is not missing . So you dump, then the script exits (hey, you've called die...). There's no chance to see the problems for the next indices.

Karoly Horvath
  • 94,607
  • 11
  • 117
  • 176