Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使数组唯一且只有一个值相同_Php_Arrays_Multidimensional Array - Fatal编程技术网

Php 使数组唯一且只有一个值相同

Php 使数组唯一且只有一个值相同,php,arrays,multidimensional-array,Php,Arrays,Multidimensional Array,我有一个数组 Array ( [1] => Array ( [id] => 1 [username] => test1 [case1] => abc [case2] => zxc ) [0] => Array ( [id] => 1 [username] => test1 [case1] => fdg

我有一个数组

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

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

)
正如您所看到的,只有id和用户名是相同的,其余的是不同的。现在我想让它独一无二。如果内部数组中只有一个id相同,那么两个数组中也只能有一个值

谁能告诉我怎么做


任何帮助都将不胜感激。

创建一个新数组,如果没有重复项,则在其中推送内部数组。

创建一个新数组,如果没有重复项,则在其中推送内部数组。

用于查找和删除数组中的特定唯一项。

用于查找和删除特定项数组中唯一的

您应该使用“id”作为顶级数组的键(或“username”?)。

您应该使用“id”作为顶级数组的键(或“username”?)。

使用唯一数据作为键可以简化:

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

使用唯一的数据作为键可以简化此操作:

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