douxing9567 2012-10-24 09:07
浏览 18
已采纳

用于比较装甲集的复杂阵列

perhaps I'm having a dumb moment, but I just can't seem to work this out in my head.

I'll try and explain:-

I have three 'sets.' Each set contains 6 objects. I need a loop (or series of nested loops) to result every combination of these sets.

It's for a game, I'm trying to write a script to compare armor sets - sad I know. The array of sets looks like this:-

class => array(
  set1 => array(
    hat item => array(stats)
    glove item => array(stats)
    ...
  set2 => array(
    hat item => array(stats)
    ...
  set3 => array(
    ...

)

This may be a lot simpler (or a lot more difficult?) than I thought.

Ideas welcome!

Thanks, Ruu.

EDIT:-

Ok here's an array sample, I've stripped out a LOT of the extra stats, and just left one for each piece:-

$setinfo = array(
    'rk' => array(
        'set1' => array(
            'hat' => array(
                'will' => 114,
            ),
            'shoulders' => array(
                'will' => 78,
            ),
            'shirt' => array(
                'will' => 78,
            ),
            'gloves' => array(
                'will' => 78,
            ),
            'trousers' => array(
                'will' => 78,
            ),
            'boots' => array(
                'will' => 114,
            ),
        ),
        'set2' => array(
            'hat' => array(
                'will' => 78,
            ),
            'shoulders' => array(
                'will' => 78,
            ),
            'shirt' => array(
                'will' => 114,
            ),
            'gloves' => array(
                'will' => 78,
            ),
            'trousers' => array(
                'will' => 114,
            ),
            'boots' => array(
                'will' => 78,
            ),
        ),
        'set3' => array(
            'hat' => array(
                'will' => 78,
            ),
            'shoulders' => array(
                'will' => 114,
            ),
            'shirt' => array(
                'will' => 78,
            ),
            'gloves' => array(
                'will' => 114,
            ),
            'trousers' => array(
                'will' => 78,
            ),
            'boots' => array(
                'will' => 78,
            ),
        ),
    )
);

From this example (I will worry about comparing other stats later,) the 'ideal' pieces would be, the hat and boots from set1, with the shirt and trousers from set2, and the gloves and shoulders from set3.

My query is how best to work out a loop which can generate a 'result' array with every permutation, for comparison later.

  • 写回答

1条回答 默认 最新

  • douchui7332 2012-10-24 09:44
    关注

    Here is an implementation from this guy:

    function array_cartesian_product($arrays)
    
    {
        $result = array();
        $arrays = array_values($arrays);
        $sizeIn = sizeof($arrays);
        $size = $sizeIn > 0 ? 1 : 0;
        foreach ($arrays as $array)
            $size = $size * sizeof($array);
        for ($i = 0; $i < $size; $i ++)
        {
            $result[$i] = array();
            for ($j = 0; $j < $sizeIn; $j ++)
                array_push($result[$i], current($arrays[$j]));
            for ($j = ($sizeIn -1); $j >= 0; $j --)
            {
                if (next($arrays[$j]))
                    break;
                elseif (isset ($arrays[$j]))
                    reset($arrays[$j]);
            }
        }
        return $result;
    }
    $combinations = array_cartesian_product($sets);
    

    Here is a simple test case: http://phpfiddle.org/main/code/m1i-w7m

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

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题