简体   繁体   中英

PHP: Every Unique Combination of 2, 3, & 4 Emoji's from a List/Array/Database

So I'm really not good with math, formula's and the like. This is a little bit above my head.

I basically have an array, or a database with about 30 emoji's in it. I want to basically enter in a number into a form, lets say 3, then hit submit. the php script will then make as many unique combinations of 3 emoji's as possible, and then place them back into an array, or even just output right onto the screen separated by a new line.

I know how to code the form, i know out to output things to the screen and place items back into the array, etc etc... I have some good experience coding, but i'm not sure how to go about creating the unqiue combinations of the emoji's based upon user input.

any help is appreciated. if any clarification is needed let me know.

The number of combinations for 3 would be 30*29*28 = 24360 for 4 you would add *27 so the total would be 657720 so this probably isn't the best idea to build as you may run out of storage space or cause a stack overflow, but for fun you could use a recursive script to build it.

$emojis = range(1, 30);
$combos = makeCombos($emojis, 3);
echo json_encode($combos);

function makeCombos($emojis, $depth, $now = []) {
    $results = [];
    $depth--;
    foreach ($emojis as $key => $value) {
        $current = $now;
        $current[] = $value;
        $emojisNew = $emojis;
        unset($emojisNew[$key]);
        if ($depth > 0) {
           $results = array_merge(
                   $results,
                   makeCombos($emojisNew, $depth, $current)
               );
        } else {
           $results[] = $current;
        }
    }
    return $results;
}

Using a range of (1, 3) so there are only 3*2*1 = 6 results yields the following result:

[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]

Note: Use $combos = makeCombos($emojis, 4); for combos of 4 emojis.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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