dongyi6845 2017-01-14 21:36
浏览 101
已采纳

PHP:列表/数组/数据库中2,3和4个表情符号的每个独特组合

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.

  • 写回答

1条回答 默认 最新

  • douchan0523 2017-01-15 00:53
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型