Merge array entries with each other entry

Sorry for the title, as it is similar to most other questions about array joining, but I don't know how to write it more specifically.

I need a PHP function that combines the entries of one array (dynamic size from 1 to any) to strings in any possible combination.

Here is an example with 4 entries:

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

This should be the result of:

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

Sorting the input array is relevant as it affects the output. And as you can see, there should be a result like e1-e2, but not e2-e1.

This seems very complicated since the input array can have any number of entries. I don’t even know if there is a mathematical construction or a name that describes such a case.

Has anyone done this before?

+4
source share
3

, , , , , . ? , , , .

PHP

+1

, , , , .
, , , . , , .

, .

array_slice , , implode - .

$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

0

here is a modified version of Search for 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;
 }

This seems to work.

0
source

Source: https://habr.com/ru/post/1690191/


All Articles