2 回答
TA贡献1827条经验 获得超7个赞
您正在为每次迭代分配一个新数组$teamUserIds。这就是您获得多个阵列的原因。
$teamUserIds = [];
foreach ($user->publishers as $userPublisher) {
foreach ($userPublisher->teams as $publisherTeam) {
$teamUserIds = array_merge($teamUserIds, $publisherTeam->members->pluck('id')->toarray());
}
}
$deDupedIds = array_unique($teamUserIds, SORT_NUMERIC);
$idsCount = count($deDupedIds);
TA贡献1775条经验 获得超8个赞
您正在将 id 数组添加$publisherTeam->members->pluck('id')->toarray();为$teamUserIds. 但你想要做的是mergeids 数组
所以你的代码会是这样的:
foreach ($user->publishers as $userPublisher) {
foreach ($userPublisher->teams as $publisherTeam) {
$teamUserIds = array_merge($teamUserIds , $publisherTeam->members->pluck('id')->toarray());
}
}
$deDupedIds = array_unique($teamUserIds, SORT_NUMERIC);
$idsCount = count($deDupedIds);
- 2 回答
- 0 关注
- 96 浏览
添加回答
举报