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 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥15 画两个图 python或R