Mahngiel Posted June 6, 2012 Share Posted June 6, 2012 I hate arrays. I really hate operating on arrays. I am looking for the best method to create a count based on the uniqueness of the following `game_id` Array ( [0] => stdClass Object ( [character_id] => 7 [user_id] => 1 [game_id] => 6 ) [1] => stdClass Object ( [character_id] => 8 [user_id] => 1 [game_id] => 15 ) [2] => stdClass Object ( [character_id] => 9 [user_id] => 1 [game_id] => 9 ) [3] => stdClass Object ( [character_id] => 10 [user_id] => 1 [game_id] => 13 ) [4] => stdClass Object ( [character_id] => 11 [user_id] => 1 [game_id] => 16 ) [5] => stdClass Object ( [character_id] => 16 [user_id] => 1 [game_id] => 6 ) ) In the above, I have 6 objects, and two have the same `game_id`. I want to create a count of games played, so I obviously want 5 to be outputted. This is what I've done get the count value: <?php if( $characters ) { $games_count = array(); foreach( $characters as $character) { $games_count[$character->game_id] = $character; } } $user->game_count = count($games_count); As I do not much like operations against arrays, I don't study much on the diff functions to use. What would be the best method to tackle this? Is there an array function that would better suit this situation? Quote Link to comment https://forums.phpfreaks.com/topic/263763-count-when-different/ Share on other sites More sharing options...
Jessica Posted June 6, 2012 Share Posted June 6, 2012 I'd have done it the way you did, I don't think there is any basic function in php that would work here. Quote Link to comment https://forums.phpfreaks.com/topic/263763-count-when-different/#findComment-1351650 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.