Get ALL possible results from mixing array

后端 未结 3 611
独厮守ぢ
独厮守ぢ 2021-01-13 02:01

I\'ve looked everywhere for this online, but couldn\'t completely find it. (my PHP and math skills are letting my down for this one...) I have an array containing for exampl

3条回答
  •  情深已故
    2021-01-13 02:14

    It looks like you're asking for the "power set". the power set includes the empty set, which i wont include.

    require_once 'Math/Combinatorics.php';
    $set = range('a', 'c');
    $permutationSizes = range(1, count($set));
    $combinatorics = new Math_Combinatorics;
    $result = array();
    foreach ($permutationSizes as $permutationSize) {
        $result = array_merge($result, $combinatorics->permutations($set, $permutationSize));
    }
    print_r($result);
    

    http://pear.php.net/package/Math_Combinatorics

    I guess technically this isnt a power set because i'm assuming order matters when comparing sets. anyway...

提交回复
热议问题