0

I'm getting an undefined index notice, though the index is defined. PHP version is 5.6

Code:

$url = $_SERVER['REQUEST_URI'];
$array = explode('?', $url);

var_dump($array);

echo "<br>".$array[0]."<br>";
echo "this is value 1:".$array[1]."<br>"; //line 9

Output:

array(2) { [0]=> string(10) "/customers" [1]=> string(9) "name=test" }
/customers
this is value 1:name=test

This all makes sense, but I also get this notice:

PHP Notice: Undefined offset: 1 in /var/source/united/magebo/api/api.php on line 9

Any idea where this notice is coming from?

I tried setting the url hard coded to the same value, the notice disappears. I found allot of questions about this issue but in my case the index IS defined.

JDel
  • 61
  • 8
  • 1
    Possible duplicate of [PHP: "Notice: Undefined variable" and "Notice: Undefined index"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – Thamilhan May 30 '16 at 09:20
  • This is not a duplicate since my index is defined... – JDel May 30 '16 at 09:24
  • provide some example , like when this notice comes and when not ? I think you put all code in one page. and you have not checked by isset function. – Niklesh Raut May 30 '16 at 09:28
  • very strange!! use foreach loop and print value. check you got one value or both. OR use array_values($array); – Ravi Hirani May 30 '16 at 09:29
  • i tried ur Code and is it working 100% without errors. there musst be another issue. what is the Code infront or after ur script ? – KikiTheOne May 30 '16 at 09:51
  • No code before or after. What is your php version? Found kind of a duplicate: http://stackoverflow.com/questions/11090328/undefined-offset-while-accessing-array-element-which-exists – JDel May 30 '16 at 09:59
  • What is the exact output of: `highlight_string(print_r($array, TRUE));`? – Rizier123 May 30 '16 at 10:27
  • Array ( [0] => /customers [1] => name=test ) I think this is just a bug... – JDel May 30 '16 at 11:43

0 Answers0