Home > OS >  Dedupe array of database results
Dedupe array of database results

Time:12-11

A result set from a PDO query is as follows...

Array
(
[0] => Array
    (
        [activity_link_type] => Category
        [data_id] => 1
    )

[1] => Array
    (
        [activity_link_type] => Category
        [data_id] => 38
    )

[2] => Array
    (
        [activity_link_type] => PData
        [data_id] => 108
    )

[3] => Array
    (
        [activity_link_type] => PData
        [data_id] => 109
    )

[4] => Array
    (
        [activity_link_type] => PData
        [data_id] => 112
    )

[5] => Array
    (
        [activity_link_type] => PData
        [data_id] => 115
    )

[6] => Array
    (
        [activity_link_type] => Role
        [data_id] => 6
    )
)

I wish to 'dedupe' the array in the simplest means (without having to do multiple nested database queries) so it looks like the following...

Array
(
[0] => Array
    (
        [activity_link_type] => Category
        [data_id] => 1
        [data_id] => 38
    )

[1] => Array
    (
        [activity_link_type] => PData
        [data_id] => 108
        [data_id] => 109
        [data_id] => 112
        [data_id] => 115
   
[2] => Array
    (
        [activity_link_type] => Role
        [data_id] => 6
    )
)

I've tried array_merge(), array_merge_recursiveand array_unique() but they either don't do anything or dedupe the wrong level of the array!

All help appreciated!

CodePudding user response:

The array you think you wanted is not possible but how about this, its fairly close and also produces an array with the activity_link_type as a key which may in fact be useful later when trying to access this array

$from_db = [
            ['activity_link_type' => 'Category', 'data_id' => 1 ],
            ['activity_link_type' => 'Category', 'data_id' => 38 ],
            ['activity_link_type' => 'PData', 'data_id' => 108 ],
            ['activity_link_type' => 'PData', 'data_id' => 112 ],
            ['activity_link_type' => 'PData', 'data_id' => 115 ],
            ['activity_link_type' => 'Role', 'data_id' => 6 ]
];

$new = [];

foreach ( $from_db as $a){
    if ( isset( $new[$a['activity_link_type']] ) ) {
        $new[$a['activity_link_type']]['data_ids'][] = $a['data_id'];
    } else {
        $new[$a['activity_link_type']] = ['activity_link_type' => $a['activity_link_type'], 
                                          'data_ids' => [$a['data_id']] ];
    }   
}
print_r($new);

RESULT

Array
(
    [Category] => Array
        (
            [activity_link_type] => Category
            [data_ids] => Array
                (
                    [0] => 1
                    [1] => 38
                )
        )
    [PData] => Array
        (
            [activity_link_type] => PData
            [data_ids] => Array
                (
                    [0] => 108
                    [1] => 112
                    [2] => 115
                )
        )
    [Role] => Array
        (
            [activity_link_type] => Role
            [data_ids] => Array
                (
                    [0] => 6
                )
        )
)
  • Related