1

I will use this function in this loop :

while ($nbrDocument < 12 && $nbrTags > 0)
{
    $tmpDocuments = $this
        ->get('fos_elastica.manager')
        ->getRepository('AppBundle:Document')
        ->findFromTag();
    $tagPossibilities = $this->generateTagPossibilities($userTags, $nbrTags);
    foreach ($tmpDocuments as $document)
    {
        $present = true;
        foreach ($tagPossibilities as $tags)
        {
            foreach ($tags as $tag)
            {
                if (!in_array($tag, $document->getTag()))
                {
                    $present = false;
                    break;
                }
            }
            if ($present) {
                break;
            }
        }
        $nbrDocument ++;
        array_push($documents, $$document);
    }
    $nbrTags--;
}

And I need to create the method generateTagPossibilities.

The first parameter contains an array of string data, and the second is the size of the possibilities I need to have.

For exemple, if I have [1][2][3][4] in my array and $nbrTag = 4, this function should return [1][2][3][4], if $nbrTag = 3, it should return [[1][2][3]] [[1][3][4]] [[2][3][4]] ...

Got any idea of how I can do that ?

Dimitri Danilov
  • 1,338
  • 1
  • 17
  • 45
  • 1
    http://stackoverflow.com/questions/19067556/php-algorithm-to-generate-all-combinations-of-a-specific-size-from-a-single-set – MBo Nov 12 '15 at 10:48
  • Just a quick FYI - you've created a (I assume accidental) [variable variable](http://php.net/manual/en/language.variables.variable.php) on this line `array_push($documents, $$document);`. – Darragh Enright Nov 12 '15 at 11:45

1 Answers1

0

You can use the following functions :

function array_hash($a)
{
    $s = '';
    foreach($a as $v)
    {
        $s.=$v.'-';
    }

    return hash('sha256',$s);
}

function removeOne($setList)
{
    $returnSetList = array();
    $hashList = array();

    foreach($setList as $set)
    {
        foreach($set as $k=>$v)
        {
            $tmpSet = $set;
            unset($tmpSet[$k]);
            $hash = array_hash($tmpSet);

            if(!in_array($hash, $hashList))
            {
                $returnSetList[] = $tmpSet;
                $hashList[] = $hash;
            }
        }
    }

    return $returnSetList;
}

function generateTagPossibilities($userTags, $nbrTags)
{
    $aUserTags = array($userTags);
    $cUserTags = count($userTags);

    if($nbrTags==$cUserTags)
        return $aUserTags;

    for($i=0; $i<($cUserTags-$nbrTags); $i++)
        $aUserTags = removeOne($aUserTags);

    return $aUserTags;
}

// Example !

$a = array(1,2,3,4);
print_r(generateTagPossibilities($a,2));

/*

Array
(
    [0] => Array
        (
            [2] => 3
            [3] => 4
        )

    [1] => Array
        (
            [1] => 2
            [3] => 4
        )

    [2] => Array
        (
            [1] => 2
            [2] => 3
        )

    [3] => Array
        (
            [0] => 1
            [3] => 4
        )

    [4] => Array
        (
            [0] => 1
            [2] => 3
        )

    [5] => Array
        (
            [0] => 1
            [1] => 2
        )
)

*/
Iansus
  • 198
  • 1
  • 12