Page 1 of 1

Incrementing Array value [SOLVED]

Posted: Sun May 07, 2006 12:16 pm
by s.dot
I am making an accept all feature for comments for blogs. Let's say the user has 2 blogs. Blog 1 has 7 comments and Blog 2 has 3 comments.

I want to get it in an array like this

Blog ID => Number of comments for that blog

Ex:

Code: Select all

Array
(
    [1] => 7
    [2] => 3
)

I have this piece of code here

Code: Select all

//	empty arrays
$final = array();
$blogids = array();

//	loop through results, and store comment ids into an array ($final);
//	and store a count of comments particular to each blog
//	BlogID => numComments
while($checka = mysql_fetch_assoc($checkr)){
	
	//	make sure they own this comment
	if($checka['userid'] == $theperson){
		
		//	they do, so add it to array
		$final[] = $checka['id'];
		
		//	if the blog id is already in the array, increment the comment count for that blog id
		if(in_array($checka['bid'],$blogids)){
			$blogids[$checka['bid']] = $blogids[$checka['bid']] + 1;
		}
		
		//	its not in the blog id array, so add it to it
		else {
			$blogids[$checka['bid']] = 1;
		}
	}
}
This code results in the following for $blogids:

Code: Select all

Array
(
    [3] => 1
    [2] => 1
)
When It should be:

Code: Select all

Array
(
    [3] => 3
    [2] => 1
)
So it looks like its not incrementing the array.

Posted: Sun May 07, 2006 1:44 pm
by feyd
I think you want array_key_exists() instead of in_array()

Posted: Sun May 07, 2006 2:19 pm
by s.dot
Ah, yes. I was thinking it would be a value, but I was clearly setting it as a key. Thanks feyd.