Jump to content

Count when different


Mahngiel

Recommended Posts

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?

 

Link to comment
https://forums.phpfreaks.com/topic/263763-count-when-different/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.