Arrays 检查对象数组php laravel中的重复条目
标识两个对象数组具有相同的密码\u代码返回布尔值如果存在true else false已尝试数组\u计数\u值仅返回字符串和数字请指导谢谢Arrays 检查对象数组php laravel中的重复条目,arrays,laravel,duplicates,Arrays,Laravel,Duplicates,标识两个对象数组具有相同的密码\u代码返回布尔值如果存在true else false已尝试数组\u计数\u值仅返回字符串和数字请指导谢谢 我只想检查数组中是否检测到相同的密码给我true else false可能的重复项不是重复项我只想检查数组中是否检测到相同的密码给我true else false收集的是什么。。?表示undefinedcollect是一个从数组创建集合的函数:Bdw我使用的是4.2。这不起作用,希望您理解我的问题,如果$users[0]['secret\u code']与$
我只想检查数组中是否检测到相同的密码给我true else false可能的重复项不是重复项我只想检查数组中是否检测到相同的密码给我true else false收集的是什么。。?表示undefinedcollect是一个从数组创建集合的函数:Bdw我使用的是4.2。这不起作用,希望您理解我的问题,如果$users[0]['secret\u code']与$users[1]['secret\u code']完全匹配,则应返回true,否则返回truefalse@Ross您在使用v4.2之前应该提到,这样,你做的工作太少,而试图回答你的人不得不做太多的猜测。
Illuminate\Database\Eloquent\Collection Object
(
[items:protected] => Array
(
[0] => User Object
(
[original:protected] => Array
(
[user_id] => 123456
[active] => 1
[name] => ABC XYZ
[first_name] => ABC
[last_name] => XYZ
[email] => abc@xyz.com
[username] => abcxyz
[secret_code] => S4#$sdD
)
)
[1] => User Object
(
[original:protected] => Array
(
[user_id] => 987654
[active] => 1
[name] => CBD IHK
[first_name] => CBD
[last_name] => IHK
[email] => abc@xyz.com
[username] => seCdils
[secret_code] => S4#$sdD
)
)
)
)
<?php
$dupes = []; // keep track of duplicates
foreach ($users as $user1) { // iterate over all items
$dupeCount = 0; // because we iterate over the same array, we always find at least the item itself (1 dupe minimum)
foreach ($users as $user2) { // check the array again
if ($user1 === $user2) { // if they are exactly the same: http://php.net/manual/en/language.oop5.object-comparison.php
$dupeCount++;
}
if ($dupeCount > 1) { // because we always find at least 1, push only when we find more than that
array_push($dupes, $user1); // add it to the result
}
}
}
collect($users)->unique(function ($item) {
return $item['secret_code'];
});