Jump to content
JackW

Notice: Undefined offset: 1

Recommended Posts

I will try to be short.  I am getting a Notice about the code below.  " Notice: Undefined offset: 1 " it refers to the last line of the code below.  That code worked for 3 years and I changed nothing, now for some reason I am getting the notice.  Can you help?

 $total_type=($row ['type1'] );

$typeChunks = (explode("|", $total_type,2));
$type2 = ($typeChunks[0]);
$type_link = ($typeChunks[1]);

Share this post


Link to post
Share on other sites

The $total_type string does not have a "|" character in it.

Any time you're dealing with an array of unknown length, you should use isset() to determine if an element exists before using it.

Share this post


Link to post
Share on other sites
15 minutes ago, Ingolme said:

The $total_type string does not have a "|" character in it.

Any time you're dealing with an array of unknown length, you should use isset() to determine if an element exists before using it.

Thank you.  That is probably the problem.  Some of the arrays contain only a single element while others have two elements separated by "|".  Those that contain 2 elements need to be separated. How would I write it to use isset()?

Share this post


Link to post
Share on other sites

I don't know what the full requirements of your software are, but your code should look something like this:

$type2 = 'Some default value';
$type_link = 'Another default value';
if(isset($typeChunks[0]) {
  $type2 = $typeChunks[0];
}
if(isset($typeChunks[1]) {
  $type_link = $typeChunks[1];
}

I'm also not sure why you've wrapped brackets around every single statement, they're not needed. There are also inconsistencies in your variable naming, you should either use all underscores ($type_link, $type_chunks) or all camel case ($typeLink, $typeChunks).

  • Thanks 1

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...