具有关联数组的唯一数组 - 删除重复项

发布于 2024-10-16 01:06:52 字数 619 浏览 2 评论 0原文

我有一个包含一些重复项目的关联数组。例如,我有:

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

输出如下所示:

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

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

我想清理整个数组,以便只有用户 John 才会出现一次(基于 user_id)。

我已尝试以下操作:

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

但它似乎不起作用。还有其他想法如何删除数组中的重复项吗?

任何帮助都会很棒!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一个人的旅程 2024-10-23 01:06:52

另一种方法是找到唯一的 user_id,重要的是它们的数组键,然后仅保留每列中的相应值。

$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);

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);
幻梦 2024-10-23 01:06:52

有几件事:

array_unique 函数不会递归到子数组中

阅读手册: http: //php.net/manual/en/function.array-unique.php
“当且仅当 (string) $elem1 === (string) $elem2 时,两个元素才被视为相等。换句话说:当字符串表示形式相同时。将使用第一个元素。”

在你的情况下 $elem1 和 $elem2 都是数组

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

渡你暖光 2024-10-23 01:06:52

一个可能的解决方案是使用 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']);

因此我们得到了数组

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

要恢复原始结构,我们只需提取键和值即可:

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

结果:

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

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

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
        )
)
乖不如嘢 2024-10-23 01:06:52

该脚本删除 user_id 上的重复项并保留多个
名字(如果 ID 不同):

$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);

输出:

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

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

)

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
        )

)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文