合并两个(或更多)数组并保留最高的值?

发布于 2024-09-09 00:12:55 字数 1006 浏览 0 评论 0原文

我有一个基于权限/组的设置,我正在尝试合并结果。例如,一个用户可能与四个组相关联:

| username  | passwd | email         | groups         |
| test_user | 9agb9  | [email protected] | g1, g2, g3, g4 |

分组表:

| group | perm1 | perm2 | perm3 | perm4 | perm5 | perm5 | perm7 |
| g1    | 1     | 0     | 0     | 0     | 1     | 0     | 2     |
| g2    | 0     | 0     | 0     | 1     | 0     | 0     | 0     |
| g3    | 0     | 1     | 0     | 0     | 2     | 0     | 0     |
| g4    | 0     | 0     | 0     | 0     | 1     | 1     | 0     |

我希望获取这四行的结果(从数据库查询存储为数组)并将它们合并到一个数组,如下所示:

| group    | perm1 | perm2 | perm3 | perm4 | perm5 | perm5 | perm7 |
| combined | 1     | 1     | 0     | 1     | 2     | 1     | 2     |

每个数组中的最高值成为组合数组中的值。如果 100 条记录中有 1 条是“2”,其余的是“1”或“0”,那么我需要在结果中包含“2”。

我查看了 array_merge,但它根据指定给函数的变量的顺序保留值。

I've got a permissions/group based setup that I'm trying to merge results on. For example, a user might be associated with four groups:

| username  | passwd | email         | groups         |
| test_user | 9agb9  | [email protected] | g1, g2, g3, g4 |

grouping table:

| group | perm1 | perm2 | perm3 | perm4 | perm5 | perm5 | perm7 |
| g1    | 1     | 0     | 0     | 0     | 1     | 0     | 2     |
| g2    | 0     | 0     | 0     | 1     | 0     | 0     | 0     |
| g3    | 0     | 1     | 0     | 0     | 2     | 0     | 0     |
| g4    | 0     | 0     | 0     | 0     | 1     | 1     | 0     |

I'm looking to take the results of those four rows (stored as an array from a DB query) and merge them down to one array, which would look like this:

| group    | perm1 | perm2 | perm3 | perm4 | perm5 | perm5 | perm7 |
| combined | 1     | 1     | 0     | 1     | 2     | 1     | 2     |

The highest value from each array becomes the value in the combined array. If 1 out of 100 records is "2" and the rest are "1" or "0" then I'd need "2" to be in the result.

I've looked at array_merge, but it keeps the value depending on the order of variables specified to the function.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

萝莉病 2024-09-16 00:12:55

也许您希望数据库已经处理这个问题:

SELECT MAX(perm1), MAX(perm2), MAX(perm3), MAX(perm4), MAX(perm5), MAX(perm6), MAX(perm7)
FROM group_perm_linking_table
WHERE group IN ('gr1', 'gr2', 'gr3', 'gr4')

Maybe you want this handled already by the DB:

SELECT MAX(perm1), MAX(perm2), MAX(perm3), MAX(perm4), MAX(perm5), MAX(perm6), MAX(perm7)
FROM group_perm_linking_table
WHERE group IN ('gr1', 'gr2', 'gr3', 'gr4')
弱骨蛰伏 2024-09-16 00:12:55

您需要创建一个新的主数组,然后循环遍历子数组,并且仅在 child['value'] > 时更新主数组。主控['值']

You'll need to make a new master array, then loop through your child arrays and only update the master when child['value'] > master['value']

千鲤 2024-09-16 00:12:55

最近不得不在 PHP 中做类似的事情:

  $new_array = array();
  foreach ($array_second as $key => $value) {
    if ($array_first[$key] > $array_second[$key]) {
      $new_array[$key] = $array_first[$key];
    }
    else {
      $new_array[$key] = $array_second[$key];
    }
  }

当然,这可以写得更简单一些,并且您可以使用三元运算符来缩短它,但是这个详细的显示显示了如何更语义地做到这一点。这是相对较快的,我用它来帮助一次合并数百个帐户设置,没有任何问题。

Had to do something similar recently in PHP:

  $new_array = array();
  foreach ($array_second as $key => $value) {
    if ($array_first[$key] > $array_second[$key]) {
      $new_array[$key] = $array_first[$key];
    }
    else {
      $new_array[$key] = $array_second[$key];
    }
  }

Of course, this can be written a little more simply, and you could use a ternary operator to shorten it, but this verbose display shows how you can do it more semantically. This is relatively fast, and I'm using it to help merge a few hundred account settings at a time without a problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文