PHP 多维数组键组合/组合学 [英] PHP multidimensional array keys combinations/combinatorics

查看:37
本文介绍了PHP 多维数组键组合/组合学的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我有一些与 PHP 相关的任务,希望能得到一些帮助.所以,假设我有这样的多维数组:

Ok, i have some assignment to do with PHP and i would appreciate some assistance. So, let's say i have multidimensional array like this one:

$testarray = array(0 => array(10, 20, 30),
                   1 => array(50, 60, 70),
                   2 => array(80, 90, 100),
                   .
                   .
                   .
                   n => array("", "", "",)

             );

数组中的值无关紧要,重要的是数组键.在这种情况下,我在每个数组元素中有 3 个键,所以当排列完成时,最终结果应该是这样的:

Values in array are irrelevant, what matters are array keys. In this case, i have 3 keys in each array element, so when permutation is finished, final result should look like this:

[0] => Array (
              [0] => 1 1 1
              [1] => 2 1 1
              [2] => 3 1 1
              [3] => 1 2 1
              [4] => 2 2 1
              [5] => 3 2 1
              [6] => 1 3 1
              [7] => 2 3 1
              . 
              .
              .
              [n] => 3 3 3
              )

如果有 4 个数组键,最终结果应该是这样的:

In case of 4 array keys, final result should look like this:

[0] = Array (
             [0] => 1 1 1 1
             [1] => 2 1 1 1
             [2] => 3 1 1 1
             [3] => 4 1 1 1
             [4] => 1 2 1 1
             .
             .
             .
             [n] => 4 4 4 4
            )

如果可能,我想避免递归.

I would like to avoid recursion if possible.

我在可视化整个循环过程和初始化所需变量时遇到问题.我真的很感激一些帮助.谢谢.

Im having problems visualizing the whole looping process and initializing needed variables. I would really appreciate some help. Thank you.

推荐答案

我对原始代码做了一些细微的修改,使其使用键的数量而不是数组值,然后我添加了第二个函数也允许对多维数组进行计数.

I made some slight modifications to the original code in order to make it use the amount of keys instead of the array values, and then I added a second function to allow a multi-dimensional array to be counted as well.

<?php
function everyCombination($array) {
  $newArray = array();
  for($keyCount = 1; $keyCount <= count($array); $keyCount++){
    $newArray[] = $keyCount;
  }
  $arrayCount      = count($newArray);
  $maxCombinations = pow($arrayCount, $arrayCount);
  $returnArray     = array();
  $conversionArray = array();
  foreach ($newArray as $key => $value) {
    $conversionArray[base_convert($key, 10, $arrayCount)] = $value;
  }
  for ($i = 0; $i < $maxCombinations; $i++) {
    $combination = base_convert($i, 10, $arrayCount);
    $combination = str_pad($combination, $arrayCount, "0", STR_PAD_LEFT);
    $returnArray[] = strtr($combination, $conversionArray);
  }
  return $returnArray;
}

function getCombos($array){
    if(is_array($array[key($array)])){
        $return = array();
        foreach($array as $subArray){
            $return[] = everyCombination($subArray);
        }
    }else{
        $return = everyCombination($array);
    }
    return $return;
}


$test = array(53,22,1233,45);

echo '<pre>';
print_r(getCombos($test));
echo '</pre>';
All credit for function and usage goes to https://stackoverflow.com/a/14022357/2285345

这篇关于PHP 多维数组键组合/组合学的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆