使数组唯一,只有一个值相同


making array unique with only one value same

我有一个数组类型为

Array
(
[1] => Array
    (
        [id] => 1
        [username] => test1
        [case1] => abc
        [case2] => zxc
    )
[0] => Array
    (
        [id] => 1
        [username] => test1
        [case1] => fdg
        [case2] => tyy
    )
)

你可以看到只有id和username是相同的,其余的是不同的。现在我想让它与众不同。如果只有id在内部数组中是相同的,那么也只有一个值应该是两者中的一个。

谁能告诉我怎么做这件事?

使用唯一数据作为键使这很容易:

$unique = array();
foreach ($array as $item) {
    $unique[$item['id']] = $item;
}

创建一个新的数组,如果没有重复的内部数组,则push

查找&删除数组array_unique

中的特定唯一性

您应该使用'id'作为top数组的键(或'username'?)