gpt4 book ai didi

php - 从数组的数组中删除重复的数组

转载 作者:可可西里 更新时间:2023-11-01 13:43:05 27 4
gpt4 key购买 nike

这是我正在处理的数组的示例:

Array
(
[0] => Array
(
[id] => 1331
[shortname] => MCS-115-113C
[userid] => 663
[email] => asdfasfd@asdfasfd.br
[username] => FOOBARBAZ
[nombrecompleto] => asdfasdf
[lastlogin] => 1367501486
)
[1] => Array
(
[id] => 1331
[shortname] => MAFA-EOOF
[userid] => 323
[email] => asdfasfd@asdfasfd.br
[username] => FOOBARBAZ
[nombrecompleto] => asdfasdf
[lastlogin] => 136732186
)
[2] => Array
(
[id] => 1331
[shortname] => MKT-FOOBAR
[userid] => 434
[email] => asdfasfd@asdfasfd.br
[username] => adsfasdf
[nombrecompleto] => asdfasdf
[lastlogin] => 1367234486
)

在我的例子中,我想比较数组中的 username 元素并删除重复项。

所以在这种情况下,我只会返回两个元素,用户名 FOOBARBAZadsfasdf:

Array
(
[0] => Array
(
[id] => 1331
[shortname] => MAFA-EOOF
[userid] => 323
[email] => asdfasfd@asdfasfd.br
[username] => FOOBARBAZ
[nombrecompleto] => asdfasdf
[lastlogin] => 136732186
)
[1] => Array
(
[id] => 1331
[shortname] => MKT-FOOBAR
[userid] => 434
[email] => asdfasfd@asdfasfd.br
[username] => adsfasdf
[nombrecompleto] => asdfasdf
[lastlogin] => 1367234486
)

我如何在 PHP 中完成此操作?

最佳答案

使用 array_unique() 很容易实现(而且速度很快,因为它是内部的) .但是,默认情况下,此函数会将所有内容转换为字符串,因此您需要传递 SORT_REGULAR 常量。 ( Demo )

<?php
$data = array(
array(
'id' => 0,
'title' => 'Abc'
),
array(
'id' => 2,
'title' => 'Def',
'content' => 'Stackoverflow!'
),
array(
'id' => 0,
'title' => 'Abc'
)
);

var_dump(array_unique($data, SORT_REGULAR));

关于php - 从数组的数组中删除重复的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16380868/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com