Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/237816-notice-undefined-index-1/
Share on other sites

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

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!

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.

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.

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.

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?  :confused: 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 :)

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?  :confused: 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.

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?

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

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.