Jump to content

explode undefined offset check?


a.mlw.walker

Recommended Posts

I am getting an undefined offset when using an explode. Usually it works however on one instance it fails. It is the second explode here that is failing, because there was no instance of " v " in the first one:

$pieces = explode(" v ", strip_tags($matches[$gameKeys[$e]]));	
echo $pieces[1]."<p>";
$findtime = explode(",", $pieces[1]);
print_r($findtime);
produces:

Tunisia, GpC, 19:00

 

Array ( [0] => Tunisia [1] => GpC [2] => 19:00 )

 

 

Notice: Undefined offset: 1 in C:\xampp\htdocs\load_data.php on line 82

 

Notice: Undefined offset: 1 in C:\xampp\htdocs\load_data.php on line 83

Array ( [0] => )

 

Crystal Palace, (agg 0-1), SF, L2, 19:45

Array ( [0] => Crystal Palace [1] => (agg 0-1) [2] => SF [3] => L2 [4] => 19:45 )

So it works on the one above and below, however one of them seems to have nothing in it, and therefore pops up an error. This won't always be the case, but sometimes this might occur. The data it is reading from shows this:

 

Morocco v Tunisia, GpC, 19:00

Qatar OFF Sweden, 13:00

Cardiff v Crystal Palace, (agg 0-1), SF, L2, 19:45

 

So you can see the Qatar one is of slightly different format - it says OFF instead of " v ". Can i run an if statement on the first one. So if there was a " v " then do the next else have something that will avoid the error occuring?

Rate this post

   

Link to comment
Share on other sites

what you can do is to check for $pieces being an array.

 

$pieces = explode(" v ", strip_tags($matches[$gameKeys[$e]]));
if(is_array($pieces)) {
   echo $pieces[1]."<p>";
   $findtime = explode(",", $pieces[1]);
   print_r($findtime);
}else {
   echo $pieces;
}	

Link to comment
Share on other sites

That is a nice proposal, however i think merely calling explode forces pieces into an array, because even if there is no " v " it still thinks pieces is an array because if I do

$pieces = explode(" v ", strip_tags($matches[$gameKeys[$e]]));
echo "<p>pieces = ". $pieces;

It returns:

pieces = Array

Link to comment
Share on other sites

That is a nice proposal, however i think merely calling explode forces pieces into an array, because even if there is no " v " it still thinks pieces is an array because if I do

$pieces = explode(" v ", strip_tags($matches[$gameKeys[$e]]));
echo "<p>pieces = ". $pieces;

It returns:

pieces = Array

yeah, sorry, not really sure why I did that, here is the revised code.

 

$pieces = explode(" v ", strip_tags($matches[$gameKeys[$e]]));
if(array_key_exists(1,$pieces)) {
   echo $pieces[1]."<p>";
   $findtime = explode(",", $pieces[1]);
   print_r($findtime);
}else {
   echo $pieces[0];
}	

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

  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.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.