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 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog