【问题标题】:Get combinations of the string [closed]获取字符串的组合[关闭]
【发布时间】:2017-05-02 06:13:11
【问题描述】:

我正在处理动态报告,我想要一些组合。考虑一下我有一个 array('1,'2','3','4') 我想要组合为 1,2 1,3 1,4 但不像 1,2,3

尝试了几乎所有方法,但没有解决方法。

【问题讨论】:

标签: php arrays combinations


【解决方案1】:

当您尝试create combinations of 2

时,这将非常适合

Try this code snippet here

<?php

ini_set('display_errors', 1);
$array = array('1', '2', '3', '4');
$combinations = array();
foreach ($array as $key1 => $value1)
{
    foreach ($array as $key2 => $value2)
    {
        if ($value1 != $value2)
        {
            $combinations[] = $value1 . "," . $value2;
        }
    }
}
print_r($combinations);

输出:

Array
(
    [0] => 1,2
    [1] => 1,3
    [2] => 1,4
    [3] => 2,1
    [4] => 2,3
    [5] => 2,4
    [6] => 3,1
    [7] => 3,2
    [8] => 3,4
    [9] => 4,1
    [10] => 4,2
    [11] => 4,3
)

【讨论】:

  • 输出匹配,但我也想要 2,1 2,3 2,4 到 4,1 4,2 4,3
  • @varadmayee 感谢您接受我的帖子... :)
猜你喜欢
  • 2021-10-27
  • 2020-11-04
  • 2023-03-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多