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
Share on other sites

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.