0

Let me explain, i need this to develop a unique title generator based on a set of words pre defined.

For example, i have this list of words:

$list = ['apple', 'banana', 'pear'];

I have a limited size of the title, for example: 1 characters If i generate a list of all permutation, i will have:

apple
banana
pear
apple banana
apple pear
apple banana pear
banana apple
banana pear
banana apple pear
pear apple
pear banana
pear banana apple

But i don't want this, ny rules are: Words cannot repeat again in another set of words and i want only the biggest set of words <= 12 characteres

The result would be:

apple banana
apple pear
banana pear

I already tried following solutions but none of these help:

PHP algorithm to generate all combinations of a specific size from a single set

PHP Find All (somewhat) Unique Combinations of an Array

Every (specific sized) combination from set/array with no duplicate items

Efficient PHP algorithm to generate all combinations / permutations of inputs

How do you generate a list of all possible strings given a generator of characters and a length?

I have this code, but it is not removing duplicates as i want

public static function search_get_combos($array = array(), $maxCaracters=12) {

        sort($array);

        $terms = array();

        for ($dec = 1; $dec < pow(2, count($array)); $dec++) {
            $curterm = array();
            foreach (str_split(strrev(decbin($dec))) as $i => $bit) {
                if ($bit) {
                    $curterm[] = $array[$i];
                }
            }
            if (!in_array($curterm, $terms) && count($curterm) > 1) {
                $title = implode(' ', $curterm);
                if (strlen($title) <= $maxCaracters){
                    $terms[$title] = $curterm;
                }
            }
        }

        return $terms;

    }

Output:

array(6) {
  ["Apple"]=>
  array(1) {
    [0]=>
    string(5) "Apple"
  }
  ["Banana"]=>
  array(1) {
    [0]=>
    string(6) "Banana"
  }
  ["Apple Banana"]=>
  array(2) {
    [0]=>
    string(5) "Apple"
    [1]=>
    string(6) "Banana"
  }
  ["Pear"]=>
  array(1) {
    [0]=>
    string(4) "Pear"
  }
  ["Apple Pear"]=>
  array(2) {
    [0]=>
    string(5) "Apple"
    [1]=>
    string(4) "Pear"
  }
  ["Banana Pear"]=>
  array(2) {
    [0]=>
    string(6) "Banana"
    [1]=>
    string(4) "Pear"
  }
}

1 Answers1

0

Do you mean this?

<?php

$list = ['apple', 'banana', 'pear', 'a'];

$result = [];

foreach($list as $k => $v) {
    unset($list[$k]);
    foreach($list as $subv) {
        $word_combination = $v.' '.$subv;
        if (strlen($word_combination) >= 10) {
            array_push($result, $v.' '.$subv);
        }
    }
}

print_r($result);
print(implode(' ', $result));

Output:

Array
(
    [0] => apple banana
    [1] => apple pear
    [2] => banana pear
)
apple banana apple pear banana pear

And I don't understand about

i want only the biggest set of words <= 10 characteres

Do you mean apple banana must be more than 10 characters (apple banana = 12 chars)? If yes, I added check for this. For example apple a (7 chars) will be ignored.

rzlvmp
  • 7,512
  • 5
  • 16
  • 45
  • For example, i generate this list: Apple banana pear Apple banana apple banana Apple banana pear > 10 characters, remove it Apple banana < 10 characters keep it apple reapeat in Apple banana, remove it banana repeat in apple banana, remove it – Dilnei Soethe Spancerski Nov 04 '20 at 14:17
  • "Apple banana < 10 characters". But Apple banana is 12 characters (or 11 without space). Do you mean 1 word == 1 character? – rzlvmp Nov 04 '20 at 14:23
  • sorry, I said wrong, it's not 10 characters, it's 12 characters. I'll try to explain it another way: let's assume that the generated titles will be: Women's Shoes Women's Shoes As the women's pumps shoe is repeated in the previous title, this must be removed. What I need in the end, is that it manages all possible combinations of titles keeping only the largest titles under 60 characters, any other title combination where all words are within a larger title must be removed from the list, end only the biggest titles that are under 60 characters. – Dilnei Soethe Spancerski Nov 04 '20 at 14:59
  • @DilneiSoetheSpancerski Ok. you can change `strlen($word_combination) >= 10` to `strlen($word_combination) >= 12` in my solution and only combinations longer than 12 symbols will be shown. At least my output same as your question's `The result would be:` – rzlvmp Nov 05 '20 at 00:45