3

I've got an associative array with some duplicate items. For example, I have:

 <? 
 $group_array = array('user_id'=>array(), 'user_first'=>array());

Which outputs something like below:

Array
 (
[user_id] => Array
    (
        [0] => 594
        [1] => 597
        [2] => 594
    )

[user_first] => Array
    (
        [0] => John
        [1] => James
        [2] => John
    )
)

I'd like to sanitize this entire array so that only the user John will show up once (based on user_id).

I've tried the following:

 <?php 
   $unique = array_unique($group_array);
   print_r($unique);

But it does not appear to work. Any other ideas how I can remove the duplicate items in the array?

Any help would be great!

4 Answers 4

3

Another approach would be to find the unique user_ids, and importantly their array keys, then keep only the corresponding values from each of the columns.

$group_array = array(
    'user_id'    => array(594,    597,     594,    598   ),
    'user_first' => array('John', 'James', 'John', 'John'),
);

// Find unique user_ids
$uniques = array_unique($group_array['user_id']);

// Keep only the uniques
foreach ($group_array as $column => $collection) {
    $group_array[$column] = array_intersect_key($collection, $uniques);
}

print_r($group_array);
Sign up to request clarification or add additional context in comments.

Comments

1

Couple of things:

the array_unique function does not recurse into sub arrays

Read the manual: http://php.net/manual/en/function.array-unique.php "Two elements are considered equal if and only if (string) $elem1 === (string) $elem2. In words: when the string representation is the same. The first element will be used."

In your case $elem1 and $elem2 are both arrays

Comments

1

A possible solution is to use the filtering side effect of array_combine(...):

$array = [
    'user_id' => [
        0 => 594,
        1 => 597,
        2 => 594
    ],
    'user_first' => [
        0 => 'John',
        1 => 'James',
        2 => 'John'
    ]
];

$combinedArray = array_combine($array['user_id'], $array['user_first']);

So we got the array

Array
(
    [594] => John
    [597] => James
)

To restore the original structure we simply can extract the keys and the values:

$array = [
    'user_id' => array_keys($combinedArray),
    'user_first' => array_values($combinedArray),
];

Result:

Array
(
    [user_id] => Array
        (
            [0] => 594
            [1] => 597
        )

    [user_first] => Array
        (
            [0] => John
            [1] => James
        )
)

Comments

0

This script removes duplicates on user_id and keeps multiple firstnames if their ids are different:

$group_array = Array(
    'user_id' => Array(
        594,
        597,
        594,
        598,
    ),
    'user_first' => Array(
        'John',
        'James',
        'John',
        'John',
    )
);

$dup = array();
for ($i=0; $i<count($group_array['user_id']); $i++) {
    if (in_array($group_array['user_id'][$i], $dup)) {
        unset($group_array['user_id'][$i]);
        unset($group_array['user_first'][$i]);
    } else {
        $dup[] = $group_array['user_id'][$i];
    }
}
print_r($group_array);

output:

Array
(
    [user_id] => Array
        (
            [0] => 594
            [1] => 597
            [3] => 598
        )

    [user_first] => Array
        (
            [0] => John
            [1] => James
            [3] => John
        )

)

Comments

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.