Incrementing Array value [SOLVED]

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Incrementing Array value [SOLVED]

Post 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.
Last edited by s.dot on Sun May 07, 2006 2:19 pm, edited 1 time in total.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I think you want array_key_exists() instead of in_array()
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Ah, yes. I was thinking it would be a value, but I was clearly setting it as a key. Thanks feyd.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Post Reply