【问题标题】:combine array entries with every other entry将数组条目与其他所有条目组合
【发布时间】:2018-05-16 22:43:38
【问题描述】:

对不起标题,因为它看起来像大多数其他关于组合数组的问题,但我不知道如何写得更具体。

我需要一个 PHP 函数,它将一个数组的条目(从 1 到任意大小的动态大小)组合成每个可能组合的字符串。

这是一个有 4 个条目的示例:

$input = array('e1','e2','e3','e4);

这应该是结果:

$result = array(
    0 => 'e1',
    1 => 'e1-e2',
    2 => 'e1-e2-e3',
    3 => 'e1-e2-e3-e4',
    4 => 'e1-e2-e4',
    5 => 'e1-e3',
    6 => 'e1-e3-e4',
    7 => 'e1-e4'
    8 => 'e2',
    9 => 'e2-e3',
   10 => 'e2-e3-e4',
   11 => 'e2-e4',
   12 => 'e3',
   13 => 'e3-e4',
   14 => 'e4'
);

输入数组的排序是相关的,因为它会影响输出。 如您所见,应该有类似e1-e2 的结果,但没有e2-e1

看起来真的很复杂,因为输入数组可以有任意数量的条目。 我什至不知道是否有描述这种情况的数学结构或名称。

以前有人做过吗?

【问题讨论】:

  • 如何构建数组的逻辑很难理解。

标签: php arrays recursion combiners


【解决方案1】:

您是说数组中可能有任意数量的条目,所以我假设您没有手动插入数据,并且会有一些源代码或代码输入数据。你能描述一下吗?根据您的要求直接存储它可能比拥有一个数组然后根据您的要求更改它更容易

这可能会有所帮助Finding the subsets of an array in PHP

【讨论】:

  • 哇,那个链接的问题或更好的答案,做正确的事。它需要对输出字符串而不是组合数组进行一些修改,但看起来它可以工作。我需要这个服务,它从用户选择的属性中选择正确的对象(复杂的计算)。我无法更改服务本身,但存储的对象和选项。例如,一辆红色汽车有carcar-red 选项。如果用户想要任何汽车,我发送选项car,如果他想要一辆红色汽车,我必须发送car-red
  • 很抱歉将其发布为答案,但在分享此内容时,我没有 50 多位声望对作者的帖子发表评论
【解决方案2】:

我已经设法将一个代码组合在一起,该代码可以根据您的输入创建您想要的输出。
我想我已经理解了每个项目何时以及为什么看起来像它的方式的逻辑。但我不确定,所以在现场使用之前要仔细测试。

我很难解释代码,因为它确实是个麻烦。

但我使用 array_slice 来获取字符串中所需的值,并在值之间添加 -

$in = array('e1','e2','e3','e4');

//$new =[];
$count = count($in);
Foreach($in as $key => $val){
    $new[] = $val; // add first value

    // loop through in to greate the long incrementing string
    For($i=$key; $i<=$count-$key;$i++){
        if($key != 0){
             $new[] = implode("-",array_slice($in,$key,$i));
        }else{
            if($i - $key>1) $new[] = implode("-",array_slice($in,$key,$i));
        }
    }

    // all but second to last except if iteration has come to far
    if($count-2-$key >1) $new[] = Implode("-",Array_slice($in,$key,$count-2)). "-". $in[$count-1];

    // $key (skip one) next one. except if iteration has come to far
    If($count-2-$key >1) $new[] = $in[$key] . "-" . $in[$key+2];

    // $key (skip one) rest of array except if iteration has come to far
    if($count-2-$key > 1) $new[] = $in[$key] ."-". Implode("-",Array_slice($in,$key+2));

    // $key and last item, except if iteration has come to far
    if($count-1 - $key >1) $new[] = $in[$key] ."-". $in[$count-1];

}


$new = array_unique($new); // remove any duplicates that may have been created

https://3v4l.org/uEfh6

【讨论】:

  • 非常感谢您的宝贵时间!它似乎工作到 4 个输入值。但不幸的是,当我添加第 5 个值时,缺少一些组合。也许我会根据您的基础找到解决方案。
  • 缺少什么组合?
  • $in = array('e1','e2','e3','e4','e5'); ist 例如e1-e3-e4e4-e5 缺失
  • 为什么会有e1-e3-e4?我认为你的问题对于输出应该是什么以及为什么应该是这样的方式非常模糊。
【解决方案3】:

这里是Finding the subsets of an array in PHP的修改版

function powerSet($in,$minLength = 1) { 
    $count = count($in); 
    $keys = array_keys($in);
    $members = pow(2,$count); 
    $combinations = array(); 
    for ($i = 0; $i < $members; $i++) { 
       $b = sprintf("%0".$count."b",$i); 
       $out = array(); 
       for ($j = 0; $j < $count; $j++) { 
          if ($b{$j} == '1') {
            $out[] = $keys[$j]; 
          }
       } 
       if (count($out) >= $minLength) { 
          $combinations[] = $out; 
       } 
    } 
    $result = array();
    foreach ($combinations as $combination) {
        $values = array();
        foreach ($combination as $key) {
            $values[$key] = $in[$key];
        }
        $result[] = implode('-', $values);
    }
    sort($result);
    return $result;
 }

这似乎有效。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-02-03
    • 1970-01-01
    • 1970-01-01
    • 2017-11-02
    • 1970-01-01
    • 2021-07-14
    • 2012-08-19
    相关资源
    最近更新 更多