siwelis Posted May 30, 2011 Share Posted May 30, 2011 This code seems to work fine in older versions of PHP, but in newer.. I get an error message... $tagArray = $temp = array(); $temp = explode(',', $words); foreach($temp as $tag){ $tagArray[trim($tag)]++; } The error message is: "Notice: Undefined index: 1..." I assume I can just turn off the error reporting, but I'd really rather fix the problem. Does anyone know what's going on? Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/237816-notice-undefined-index-1/ Share on other sites More sharing options...
mikesta707 Posted May 30, 2011 Share Posted May 30, 2011 you never give your variable $tarArray any other value but an empty array. You haven't given enough information to give any insight into what $tarArray should contain. Quote Link to comment https://forums.phpfreaks.com/topic/237816-notice-undefined-index-1/#findComment-1222090 Share on other sites More sharing options...
siwelis Posted May 30, 2011 Author Share Posted May 30, 2011 how could i better declare $tagArray? Quote Link to comment https://forums.phpfreaks.com/topic/237816-notice-undefined-index-1/#findComment-1222096 Share on other sites More sharing options...
seanlim Posted May 30, 2011 Share Posted May 30, 2011 if i'm not mistaken, $tagArray[1] is undefined, so $words must be containing "1" somewhere? You will need to initialize $tagArray[$tag] if it isn't already initialized before incrementing it. $tagArray = $temp = array(); $temp = explode(',', $words); foreach($temp as $tag){ if(!array_key_exists($tag, $tagArray)) $tagArray[$tag] = 0; $tagArray[trim($tag)]++; } Quote Link to comment https://forums.phpfreaks.com/topic/237816-notice-undefined-index-1/#findComment-1222097 Share on other sites More sharing options...
fugix Posted May 30, 2011 Share Posted May 30, 2011 what are you trying to set $tagArray to exactly here? Quote Link to comment https://forums.phpfreaks.com/topic/237816-notice-undefined-index-1/#findComment-1222099 Share on other sites More sharing options...
siwelis Posted May 30, 2011 Author Share Posted May 30, 2011 seanlim, I ended up using the function array_key_exists you used. It's the first time I've ever used that function... I was trying to do that with isset but was having a hard time figuring it out... Nice to know there's a function for it already. Thank you for your help everyone! Quote Link to comment https://forums.phpfreaks.com/topic/237816-notice-undefined-index-1/#findComment-1222102 Share on other sites More sharing options...
mikesta707 Posted May 30, 2011 Share Posted May 30, 2011 While this will fix your problem, it doesn't really address the issue of why your $tagArray is empty, and what it is meant to contain originally. If this script has been like this since its creation (you mention it running on previous versions of PHP without problem. This is probably because you had error reporting turned off, or set up in such a way to not report Notices), I guess it would be ok to leave it as is, but you may want to consider getting to the root of the problem. However, if your script works, and your happy with the result, then by all means leave it as is, and work on something else that requires your attention. Quote Link to comment https://forums.phpfreaks.com/topic/237816-notice-undefined-index-1/#findComment-1222105 Share on other sites More sharing options...
seanlim Posted May 30, 2011 Share Posted May 30, 2011 While this will fix your problem, it doesn't really address the issue of why your $tagArray is empty, and what it is meant to contain originally. I believe it sufficiently addresses the issue, since it is explicitly declared to be an empty array from the start. $tagArray is empty simply because he declared it so, and it was not meant to contain anything, but to store the count of the occurrence of each tag. The only reason why he has to check for an existing key (admittedly a less-orthodox method) before initialization is because he is using an associative array with an unknown set of keys, and thus cannot declare them prior to the loop. Pardon me if I'm wrong, but I don't see how there is any further "root" to the problem, apart from redesigning the entire block of code, which I think is already rather lean. Quote Link to comment https://forums.phpfreaks.com/topic/237816-notice-undefined-index-1/#findComment-1222119 Share on other sites More sharing options...
mikesta707 Posted May 30, 2011 Share Posted May 30, 2011 While this will fix your problem, it doesn't really address the issue of why your $tagArray is empty, and what it is meant to contain originally. I believe it sufficiently addresses the issue, since it is explicitly declared to be an empty array from the start. $tagArray is empty simply because he declared it so, and it was not meant to contain anything, but to store the count of the occurrence of each tag. The only reason why he has to check for an existing key (admittedly a less-orthodox method) before initialization is because he is using an associative array with an unknown set of keys, and thus cannot declare them prior to the loop. Pardon me if I'm wrong, but I don't see how there is any further "root" to the problem, apart from redesigning the entire block of code, which I think is already rather lean. Yes, I understand that it is meant to be a counter of some sort, but the root of the problem is why he would set it to an empty array. To me at least, this suggests OP is unsure of what he means to do with this variable. For example, you use array_key_exists, but there is really no point since in the context it will be used, it will always be true. He could simply just set $tagArray[$tag] to 1. OP could also accomplish the same task by simply using array_count_values() which would eliminate the foreach loop all together. But perhaps there is a reason he has the foreach loop that we cannot see because we don't know what else is going on in OPs script. My point is that OP fixed the problem with somewhat pointless code (in the context, I can't say definitely because I don't know what this snippet fits into) And while he won't have any errors or notices, perhaps there is a better way to do what he wants if he examines what exactly it is he is trying to do. Quote Link to comment https://forums.phpfreaks.com/topic/237816-notice-undefined-index-1/#findComment-1222122 Share on other sites More sharing options...
seanlim Posted May 30, 2011 Share Posted May 30, 2011 For example, you use array_key_exists, but there is really no point since in the context it will be used, it will always be true. Will it always be true? i added in that line to initialize the key when it has not yet been initialized. Unless my logic is flawed... But ok, I agree that given what little code we have, my solution simply fills in the gap to complete the OP's logic and might not be the ideal way to act as a counter Quote Link to comment https://forums.phpfreaks.com/topic/237816-notice-undefined-index-1/#findComment-1222140 Share on other sites More sharing options...
mikesta707 Posted May 30, 2011 Share Posted May 30, 2011 For example, you use array_key_exists, but there is really no point since in the context it will be used, it will always be true. Will it always be true? i added in that line to initialize the key when it has not yet been initialized. Unless my logic is flawed... But ok, I agree that given what little code we have, my solution simply fills in the gap to complete the OP's logic and might not be the ideal way to act as a counter yes in the context, since OP used explode, which will return a numeric array, array_key_exists will always be true. Quote Link to comment https://forums.phpfreaks.com/topic/237816-notice-undefined-index-1/#findComment-1222143 Share on other sites More sharing options...
seanlim Posted May 30, 2011 Share Posted May 30, 2011 Okay, I just pulled an all-nighter so my mind's all fudged up... sorry if my logic's screwed, but I still don't see how it will always be true! <?php $words = "foo,bar,test,foo"; $tagArray = $temp = array(); $temp = explode(',', $words); foreach($temp as $tag){ var_dump(!array_key_exists($tag, $tagArray)); if(!array_key_exists($tag, $tagArray)) $tagArray[$tag] = 0; $tagArray[trim($tag)]++; } print_r($tagArray); ?> Output: bool(true) bool(true) bool(true) bool(false) Array ( [foo] => 2 [bar] => 1 [test] => 1 ) Maybe I misunderstand you? Quote Link to comment https://forums.phpfreaks.com/topic/237816-notice-undefined-index-1/#findComment-1222153 Share on other sites More sharing options...
mikesta707 Posted May 30, 2011 Share Posted May 30, 2011 oh my mistake, I was under the impression that $tag was an array key instead of an array value for some reason. Don't worry im pretty tired also. My point still stands that he could simply use array_count_values to make the foreach irrelevant, and that OP making this counter variable an empty array seems to suggest that his intent for that variable is unclear. It really doesnt matter in the long run Quote Link to comment https://forums.phpfreaks.com/topic/237816-notice-undefined-index-1/#findComment-1222158 Share on other sites More sharing options...
seanlim Posted May 30, 2011 Share Posted May 30, 2011 Yep, agreed. array_count_values is definitely more efficient. Cheers mate Quote Link to comment https://forums.phpfreaks.com/topic/237816-notice-undefined-index-1/#findComment-1222163 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.