I'm trying figure why I'm getting this error Undefined offset: 1.

I'm practicing with list() function and with a explode() function.

When I ran the code it kept mention this error:

Undefined offset: 1 

The line is this:

list($bagsize, $candyprice)=explode("#", $_GET["size"]);

The list is not an array. It's on the database:

$bagsize = $_GET["size"]);
$candyprice = $_GET["price"]);

But the thing is that I don't understand why is it offset.

I google it and read a few threads and find out it varies.

I just want to prevent this error from happening again.

Any Suggestions and explanations will help. I appreciate it. Thanks!

4 Years
Discussion Span
Last Post by LastMitch

Notice: Undefined offset: 1

You're not getting an Error, but a Notice.This is caused because your $_GET["size"] doesn't contain "#" anywhere in the string so it contains only one element in array.list() is used to assign a list of variables in one operation.Your list contains 2 elements but as from data returned by implode,there is only one data.So it throws a notice.
A way to overcome that is to use array_pad() method.

list($bagsize, $candyprice)= array_pad(explode("#", $_GET["size"]),2,null);

Edited by IIM

Votes + Comments
Thanks for the explanation & solution!


Thanks for the reply and explanation. I'll be honest with you I don't get much of these Notice mostly I get is an error so I know how to debug it. But for this thing (notice) I have a hard time understanding. I will make some adjustment.



It worked! The Notice disappear. I just want to say Thanks for the explanation and solution.

The array_pad() function was something new! I don't used that often.

I learn something new I appreciated your help! Thanks


@lastmitch: echo the content of $_GET["size"] for which you are getting error.Also post it here.


@lastmitch: echo the content of $_GET["size"] for which you are getting error

There's no Notice nor error. Everything is good! Thanks. IIM.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.