a.mlw.walker Posted January 23, 2012 Share Posted January 23, 2012 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 https://forums.phpfreaks.com/topic/255590-explode-undefined-offset-check/ Share on other sites More sharing options...
AyKay47 Posted January 23, 2012 Share Posted January 23, 2012 because if there isn't a "v" in $matches[$gameKeys[$e]], $pieces will not hold an array, and using $pieces[1] will be invalid. Link to comment https://forums.phpfreaks.com/topic/255590-explode-undefined-offset-check/#findComment-1310284 Share on other sites More sharing options...
a.mlw.walker Posted January 23, 2012 Author Share Posted January 23, 2012 So is there a way to check whether calling the fucntion explode returns succesful rather than generating an error? Link to comment https://forums.phpfreaks.com/topic/255590-explode-undefined-offset-check/#findComment-1310298 Share on other sites More sharing options...
AyKay47 Posted January 23, 2012 Share Posted January 23, 2012 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 https://forums.phpfreaks.com/topic/255590-explode-undefined-offset-check/#findComment-1310306 Share on other sites More sharing options...
a.mlw.walker Posted January 23, 2012 Author Share Posted January 23, 2012 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 https://forums.phpfreaks.com/topic/255590-explode-undefined-offset-check/#findComment-1310315 Share on other sites More sharing options...
Pikachu2000 Posted January 23, 2012 Share Posted January 23, 2012 Yes, explode normally returns an array. If the delimiter isn't present in the string, the array will have only one element. That behavior is documented in the manual entry. Link to comment https://forums.phpfreaks.com/topic/255590-explode-undefined-offset-check/#findComment-1310319 Share on other sites More sharing options...
a.mlw.walker Posted January 23, 2012 Author Share Posted January 23, 2012 So is there no way for me to know whether the explode was succesful in terms of whether it found the delimiter and "exploded" it? Ah I used a strpos. Thanks Link to comment https://forums.phpfreaks.com/topic/255590-explode-undefined-offset-check/#findComment-1310324 Share on other sites More sharing options...
AyKay47 Posted January 23, 2012 Share Posted January 23, 2012 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 https://forums.phpfreaks.com/topic/255590-explode-undefined-offset-check/#findComment-1310339 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.