简体   繁体   English

将数组条目与其他条目组合在一起

[英]combine array entries with every other entry

Sorry for the title as it looks like most of the other questions about combining arrays, but I don't know how to write it more specific. 抱歉标题,因为它看起来像关于组合数组的大多数其他问题,但我不知道如何更具体地写它。

I need a PHP function, which combines the entries of one array (dynamic size from 1 to any) to strings in every possible combination. 我需要一个PHP函数,它将一个数组(动态大小从1到任意)的条目组合成每种可能组合的字符串。

Here is an example with 4 entries: 这是一个包含4个条目的示例:

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

This should be the result: 这应该是结果:

$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'
);

The sorting of the input array is relevant as it affects the output. 输入数组的排序是相关的,因为它会影响输出。 And as you see, there should be an result like e1-e2 but no e2-e1 . 如你所见,应该有像e1-e2但没有e2-e1

It seems really complicated, as the input array could have any count of entries. 这似乎很复杂,因为输入数组可以有任何条目计数。 I don't even know if there is a mathematical construct or a name which describes such a case. 我甚至不知道是否有数学结构或描述这种情况的名称。

Has anybody done this before? 有没有人这样做过?

You are saying that there might be any number of entries in the array so I'm assuming that you aren't manually inserting the data and there would be some source or code entering the data. 你是说数组中可能有任意数量的条目,所以我假设你没有手动插入数据,并且会有一些源或代码输入数据。 Can you describe that? 你能描述一下吗? It might be easier to directly store it as per your requirement than having an array and then changing it as per your requirement 根据您的要求直接存储它可能比拥有一个数组然后根据您的要求更改它更容易

This might be helpful Finding the subsets of an array in PHP 这可能有助于在PHP中查找数组的子集

I have managed to bodge together a code that creates the output you want from the input you have. 我已经设法将一个代码组合在一起,从您输入的输入中创建所需的输出。
I think I have understood the logic of when and why each item looks the way it deos. 我想我已经理解了每个项目何时以及为什么看起来像它的方式的逻辑。 But Im not sure, so test it carefully before using it live. 但我不确定,所以在使用它之前要仔细测试一下。

I have a hard time explaining the code since it's really a bodge. 我很难解释代码,因为它真的很混乱。

But I use array_slice to grab the values needed in the strings, and implode to add the - between the values. 但我使用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 https://3v4l.org/uEfh6

here is a modificated version of Finding the subsets of an array in PHP 这是在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;
 }

This seems to work. 这似乎有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM