如何在 PHP 中找到数组的所有可能组合

发布于 2024-10-09 11:51:10 字数 628 浏览 0 评论 0原文

$data = array(
'a' => array('a1', 'a2', 'a3'),
'b' => array('b1', 'b2', 'b3', 'b4'),
'c' => array('c1', 'c2', 'c3', 'c4', 'c5'));

得到

a1
a2
a3
b1
b2
b3
b4
c1
c2
c3
c4
c5

a1 b1
a1 b2
a1 b3
a1 b4
a1 c1
a1 c2
a1 c3
a1 c4
a1 c5

b1 c1
b1 c2
b1 c3
b1 c4
b1 c5
b2 c1
b2 c2
b2 c3
b2 c4
b2 c5
b3 c1
b3 c2
b3 c3
b3 c4
b3 c5
b4 c1
b4 c2
b4 c3
b4 c4
b4 c5

a1 b1 c1
a1 b1 c2
a1 b1 c3
a1 b1 c4
a1 b1 c5
a1 b2 c1
a1 b2 c2
a1 b2 c3
a1 b2 c4
a1 b2 c5
a1 b3 c1
a1 b3 c2
a1 b3 c3
a1 b3 c4
a1 b3 c5
a1 b4 c1
a1 b4 c2
a1 b4 c3
a1 b4 c4
a1 b4 c5
etc...

谢谢

$data = array(
'a' => array('a1', 'a2', 'a3'),
'b' => array('b1', 'b2', 'b3', 'b4'),
'c' => array('c1', 'c2', 'c3', 'c4', 'c5'));

to get

a1
a2
a3
b1
b2
b3
b4
c1
c2
c3
c4
c5

a1 b1
a1 b2
a1 b3
a1 b4
a1 c1
a1 c2
a1 c3
a1 c4
a1 c5

b1 c1
b1 c2
b1 c3
b1 c4
b1 c5
b2 c1
b2 c2
b2 c3
b2 c4
b2 c5
b3 c1
b3 c2
b3 c3
b3 c4
b3 c5
b4 c1
b4 c2
b4 c3
b4 c4
b4 c5

a1 b1 c1
a1 b1 c2
a1 b1 c3
a1 b1 c4
a1 b1 c5
a1 b2 c1
a1 b2 c2
a1 b2 c3
a1 b2 c4
a1 b2 c5
a1 b3 c1
a1 b3 c2
a1 b3 c3
a1 b3 c4
a1 b3 c5
a1 b4 c1
a1 b4 c2
a1 b4 c3
a1 b4 c4
a1 b4 c5
etc...

Thanks

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

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

发布评论

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

评论(2

巷雨优美回忆 2024-10-16 11:51:10

显然,您想要构建多个数组的笛卡尔积,即每个元素与其他元素相结合。

此外,您希望结果元组省略一个或多个数组,为了简单起见,我将其建模为每个数组中都有一个 null 元素:

$result = array(array()); // We need to start with one element already, because thats the identity element of the cartesian product
foreach ($data as $arr)
{
    array_push($arr,null); // Add a null element to the array to get tuples with less than all arrays

    // This is the cartesian product:
    $new_result = array();
    foreach ($result as $old_element)
        foreach ($arr as $el)
            $new_result []= array_merge($old_element,array($el));
    $result = $new_result;
}

请注意,对于您的结果行 a1 b3 c2 此代码为您提供 array('a1','b3','c2') 以及结果行 b4 c3 此代码为您提供array('b4','c3',null)

Apparently you want to build the cartesian product of several arrays, i.e. every element combined with each other element.

In addition, you want to have result tuples that omit one or more of those arrays which, for the sake of simplicity, I would model as having a null element in each of those arrays:

$result = array(array()); // We need to start with one element already, because thats the identity element of the cartesian product
foreach ($data as $arr)
{
    array_push($arr,null); // Add a null element to the array to get tuples with less than all arrays

    // This is the cartesian product:
    $new_result = array();
    foreach ($result as $old_element)
        foreach ($arr as $el)
            $new_result []= array_merge($old_element,array($el));
    $result = $new_result;
}

Note that for your result line a1 b3 c2 this code gives you array('a1','b3','c2') and for your result line b4 c3 this code gives you array('b4','c3',null).

烟火散人牵绊 2024-10-16 11:51:10

如果您想将它们全部打印出来,只需使用循环:

foreach($data['a'] as $k1 =>$v1){
    $output[]=$v1;
    foreach($data['b'] as $k2 => $v2){
        $output[]=$v2;
        $output[]=$v1."-".$v2;
        foreach($data['c'] as $k3 => $v3){
            $output[]=$v3;
            $output[]=$v1."-".$v2."-".$v3;
        }
    }
} 

http://www.webdeveloper.com/forum/showthread.php?t=168409" rel="nofollow">http://www. webdeveloper.com/forum/showthread.php?t=168409

Google 在这方面非常棒...

如果您想查看有多少种可能性,请将它们相乘:

$count1=1;
$count2=1;
for each $data as $item{
$count2*=count($item);
}

If you want to print them all out, just use loops:

foreach($data['a'] as $k1 =>$v1){
    $output[]=$v1;
    foreach($data['b'] as $k2 => $v2){
        $output[]=$v2;
        $output[]=$v1."-".$v2;
        foreach($data['c'] as $k3 => $v3){
            $output[]=$v3;
            $output[]=$v1."-".$v2."-".$v3;
        }
    }
} 

http://www.webdeveloper.com/forum/showthread.php?t=168409

Google is awesome that way...

If you want to see how many possibilities there are, multiply them:

$count1=1;
$count2=1;
for each $data as $item{
$count2*=count($item);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文