Array with associative array - duplicate removal

I have an associative array with multiple duplicate elements. 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 would like to misinform this whole array so that only one John user appears once (based on user_id).

I tried the following:

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

But it does not work. Any other ideas how I can remove duplicate elements in an array?

Any help would be great!

+3
source share
4 answers

Another approach would be to find unique user_ids and, most importantly, their array keys, and then save only the corresponding values ​​from each column.

$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);
+3
source

A couple of things:

array_unique function does not recurs to submatrices

: http://php.net/manual/en/function.array-unique.php " , (string) $elem1 === (string) $elem2. : , .

$elem1 $elem2

+1

This script removes duplicates on user_id and saves several firstnames if their identifiers 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
        )

)
0
source

A possible solution is to use a side effect of filtering 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 an array

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

To restore the original structure, we can simply extract the keys and 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
        )
)
0
source

Source: https://habr.com/ru/post/1789440/


All Articles