Jump to content

Notice: Undefined offset: 1


JackW

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]);

Link to comment
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.

Link to comment
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()?

Link to comment
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
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...