1

I would like to calculate duplicate values in my array by "groupid":

Example:

Array
(
    [0] => Array
        (
            [id] => 1230
            [groupid] => 177
            [activity_group_last] => 1229
            [name] => First name
        )

    [1] => Array
        (
            [id] => 1231
            [groupid] => 177
            [activity_group_last] => 1229
            [name] => Second name
        )

    [2] => Array
        (
            [id] => 1232
            [groupid] => 178
            [activity_group_last] => 1229
            [name] => Other name
        )

)

Output array (2 groupid = 177 and 1 groupid = 178):

Array
(
    [0] => Array
        (
            [id] => 1231
            [groupid] => 177
            [activity_group_last] => 1229
            [name] => Second name
            [count] => 2

        )

    [1] => Array
        (
            [id] => 1232
            [groupid] => 178
            [activity_group_last] => 1229
            [name] => Other name
            [count] => 1
        )
)

Thanks!

5
  • Um..calculate? what kind of calculation, and on what fields? Please be more specific Commented Aug 11, 2011 at 7:10
  • There seems to be some data-loss in your example: Only 1 id value and 1 name value is kept when the 2 arrays with identical groupids are grouped. Is that on purpose? Commented Aug 11, 2011 at 7:14
  • It's right example. Just calculate groupids count and remove duplicate. Commented Aug 11, 2011 at 7:22
  • If it happens that your array is populated by a SQL query, please do this from the query, not with some PHP code. Commented Aug 11, 2011 at 8:05
  • This is my query: $sql = 'SELECT wall.id, members.groupid, last.activity_group_last, groups.name FROM #__community_wall AS wall, #__community_groups_members AS members, #__mod_table AS last, #__community_groups AS groups WHERE members.memberid ='.intval($uid).' AND members.approved = 1 AND members.groupid = wall.contentid AND wall.post_by !='.intval($uid).' AND wall.type="groups" AND wall.id > last.activity_group_last AND last.userid ='.intval($uid).' AND last.activity_group_last > 0 AND last.activity_group_last_new = "0" AND groups.id = members.groupid ORDER BY wall.id'; Commented Aug 11, 2011 at 8:42

2 Answers 2

1

If $value contains your array, then:

$count = array_count_values(array_map(function($item) {
    return $item['groupid'];
}, $value));
var_dump($count);

$_tmp = $count;
$unique = array_filter($value, function(&$item) use (&$_tmp, $count) {
    if (!--$_tmp[$item['groupid']]) {
        $item['count'] = $count[$item['groupid']];
        return true;
    }
    return false;
});
var_dump($unique);

results in:

array(2) {
  [1]=>
  array(5) {
    ["id"]=>
    int(1231)
    ["groupid"]=>
    int(177)
    ["activity_group_last"]=>
    int(1229)
    ["name"]=>
    string(11) "Second name"
    ["count"]=>
    int(2)
  }
  [2]=>
  array(5) {
    ["id"]=>
    int(1232)
    ["groupid"]=>
    int(178)
    ["activity_group_last"]=>
    int(1229)
    ["name"]=>
    string(10) "Other name"
    ["count"]=>
    int(1)
  }
}
Sign up to request clarification or add additional context in comments.

3 Comments

+1, but you could add some comments, unless you aspire for the "Most Obscure Code Snippet of the Day" -award ;-)
But I need to add: [count] => X to output array
Yes my friend, you are the rock!
0

You can also do this using an iterative function. If your input array is stored in $input and you want the results in $output:

function remove_duplicates($input_array) {
  $output_array = array();            // Create an empty array for output
  foreach($input_array as $input) {   // Loop the input array
    if(array_key_exists($input['groupid'], $output_array)) {
      // We've already seen this groupid at least once
      // Increment count
      $input['count'] = $output_array[$input['groupid']]['count'] + 1;
    } else {
      // First time we've seen this groupid
      // Set count to 1
      $input['count'] = 1;
    }
    // Store data in $output_array, indexed by group_id
    $output_array[$input['groupid']] = $input;
  }
}

// This is your input array
$input = array(array('id'=>1230,'groupid'=>177,'activity_group_last'=>1229,'name'=>'First name'),
           array('id'=>1231,'groupid'=>177,'activity_group_last'=>1229,'name'=>'Second name'),
           array('id'=>1232,'groupid'=>178,'activity_group_last'=>1229,'name'=>'Other name'));
// This will set the output array correctly
$output = remove_duplicates($input);

3 Comments

The is a problem with this function. It output: Array ( [] => Array ( [id] => 1279 [groupid] => 177 [activity_group_last] => 1275 [name] => Name [count] => 4 ) ) But we have other array (input): [2] => Array ( [id] => 1232 [groupid] => 178 [activity_group_last] => 1229 [name] => Other name )
The is a problem with this function. It output: Array ( [] => Array ( [id] => 1279 [groupid] => 177 [activity_group_last] => 1275 [name] => Name [count] => 4 ) )
Script is updated to set $input to the values you originally provided; in a quick test, it works fine. Can you be more specific with the problem you're seeing?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.