PHP返回不同组的结果

发布于 2024-11-14 13:09:38 字数 358 浏览 2 评论 0原文

然后

我有一堆数据,例如 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

我将这些数据分为 2 组,例如

$groupA = range(1, 5)

$groupB = range(6, 10)

,我有 < code>$data = array(1, 4) 它将返回属于 A 组的数据。同样, $data = array(7,8),它会返回给我B组。

那么我怎样才能编写一个脚本让$data = array(1, 4, 6, 7) 返回 A 组和 B 组?

谢谢

all

I have a bundle of data like 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

then I separate these data into 2 groups which is

$groupA = range(1, 5)

$groupB = range(6, 10)

For instance, I have $data = array(1, 4) and it will return this belong to Group A. Likewise,
$data = array(7,8), it will return to me Group B.

So how can I write a script to let $data = array(1, 4, 6, 7) return me Group A and Group B?

Thank you

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

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

发布评论

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

评论(4

风启觞 2024-11-21 13:09:38

您可能需要使用 array_intersect

$groupA = range(1, 5);
$groupB = range(6, 10);
$data = array(1, 4, 6, 7);
$foundGroups = array();
if(array_intersect($data, $groupA))
    $foundGroups[] = 'A';
if(array_intersect($data, $groupB))
    $foundGroups[] = 'B';
print_r($foundGroups);

请注意一个空数组评估为false 而至少包含一个元素的一个计算结果为 true

警告:如果您必须使用大量组来工作对于许多元素,您可能希望使用手动方法并在找到的第一个公共元素处停止。 array_intersect 找到所有公共元素,但您实际上并不需要它。

You may want to use array_intersect:

$groupA = range(1, 5);
$groupB = range(6, 10);
$data = array(1, 4, 6, 7);
$foundGroups = array();
if(array_intersect($data, $groupA))
    $foundGroups[] = 'A';
if(array_intersect($data, $groupB))
    $foundGroups[] = 'B';
print_r($foundGroups);

Note that an empty array evaluates to false while one with at least one element evaluates to true.

Warning: If you have to work with a lot of groups with many elements you may want to use a manual approach and stop at the first common element found. array_intersect finds all the common elements and you don't really need that.

舟遥客 2024-11-21 13:09:38

你的意思是这样的吗?

$data = array(1, 4, 6, 7)
$groupA = array();
$groupB = array();

foreach ((array) $data as $value) {
    if ($value < 6) {
        $groupA[] = $value;
    } else {
        $groupB[] = $value;
    }
}

Greetz,

XpertEase

Do you mean something like this?

$data = array(1, 4, 6, 7)
$groupA = array();
$groupB = array();

foreach ((array) $data as $value) {
    if ($value < 6) {
        $groupA[] = $value;
    } else {
        $groupB[] = $value;
    }
}

Greetz,

XpertEase

GRAY°灰色天空 2024-11-21 13:09:38

尝试对每个组使用 array_intersect ...如果交集不为空,则意味着某些元素位于这组...

Try to use array_intersect with every group... if the intersection in not null it means that some elements are in this group...

我一向站在原地 2024-11-21 13:09:38
$data = range(1,9);

$groupA = array_filter($data, "less");
$groupB = array_filter($data, "more");


function less ($v) {
        return $v < 6;
}

function more ($v) {
        return ! less($v);      
}

查看

$data = range(1,9);

$groupA = array_filter($data, "less");
$groupB = array_filter($data, "more");


function less ($v) {
        return $v < 6;
}

function more ($v) {
        return ! less($v);      
}

See it

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